array_combine modification

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jjfletch
Forum Newbie
Posts: 13
Joined: Mon Apr 18, 2005 5:42 am

array_combine modification

Post by jjfletch »

Is there a way to have the effect of array_combined, but ensure that duplicate elements of an array are not removed?


For example, the way things are now, this happens:

Code: Select all

Array ( [name] => Array ( [0] => Joe [1] => Joe [2] => Joe ) [animal] => Array ( [0] => Dog  [1] => Cat [2] => Rabbit  ) [submit] => Validez )
Returns:

Code: Select all

Array ( [Joe] => Dog [] => )

What I would want is for it to return:

Code: Select all

Array ( [Joe] => Dog [Joe] => Cat  [Joe] => Rabbit)

Upon array_combine.

This is the array_combine function I'm using:

Code: Select all

if (!function_exists('array_combine')) {
   function array_combine($a, $b) {
       $c = array();
       if (is_array($a) && is_array($b))
           while (list(, $va) = each($a))
               if (list(, $vb) = each($b))
                   $c[$va] = $vb;
               else
                   break 1;
       return $c;
   }
}
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Re: array_combine modification

Post by shneoh »

I am not very sure, but so far I know is each array reference should have identical name. If you really want this, I suggest you change this into 3 dimentional array which returns the result as:

[1]=>[Joe]=>Dog
[2]=>[Joe]=>Cat
[3]=>[Joe]=>Rabbit

A quick fix:

Code: Select all

if (!function_exists('array_combine')) {
   function array_combine($a, $b) {
       $c = array(); $ct=0;
       if (is_array($a) && is_array($b))
           while (list(, $va) = each($a))
               if (list(, $vb) = each($b)){
                   $c[$ct][$va] = $vb;
                   $ct++;
               }else
                   break 1;
       return $c;
   }
}
Post Reply