To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > General Help

General Help Forum for General Help questions pertaining to PHP

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 01-18-2008, 02:55 AM   #1
ade234uk
Still not professional
 
Join Date: Aug 2006
Posts: 167
resolved [RESOLVED] trying to combine two arrays as key value pairs php4

I am trying to combine two arrays in php4 as key value pairs. I found an example that does the job, however array_combine does not work in php4.

How do I combine to arrays in php4 as key value pairs.


$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
ade234uk is offline   Reply With Quote
Old 01-18-2008, 03:22 AM   #2
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 12,638
PHP Code:
function my_array_combine($a, $b)
{
   
$result = array();
   while((
$key = each($a)) && ($val = each($b)))
   {
      
$result[$key[1]] = $val[1];
   }
   return(
$result);
}
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett

Email me
NogDog is offline   Reply With Quote
Old 01-18-2008, 03:34 AM   #3
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 12,638
PS: If you want a more robust version which emulates the error results/notices of the PHP5 function:
PHP Code:
function my_array_combine($a, $b)
{
   if(!
is_array($a) or !is_array($b) or !count($a) or !count($b))
   {
      
user_error(__FUNCTION__ . ': non-array or empty array supplied as parameter');
      return(
FALSE);
   }
   if(
count($a) != count($b))
   {
      
user_error(__FUNCTION__ . ': empty array supplied as parameter');
   }
   
$result = array();
   while((
$key = each($a)) && ($val = each($b)))
   {
      
$result[$key[1]] = $val[1];
   }
   return(
$result);
}
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett

Email me
NogDog is offline   Reply With Quote
Old 01-18-2008, 04:25 AM   #4
ade234uk
Still not professional
 
Join Date: Aug 2006
Posts: 167
Brillliant, thank you very much
ade234uk is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 02:33 AM.








Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.