jjfletch Posted September 26, 2006 Share Posted September 26, 2006 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=php:0]Array ( [name] => Array ( [0] => Joe [1] => Joe [2] => Joe ) [animal] => Array ( [0] => Dog [1] => Cat [2] => Rabbit ) [submit] => Validez )[/code]Returns:[code=php:0]Array ( [Joe] => Dog [] => )[/code]What I would want is for it to return:[code=php:0]Array ( [Joe] => Dog [Joe] => Cat [Joe] => Rabbit)[/code]Upon array_combine. This is the array_combine function I'm using:[code=php:0]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; }}[/code] Link to comment https://forums.phpfreaks.com/topic/22072-array_combine-modification/ Share on other sites More sharing options...
extrovertive Posted September 26, 2006 Share Posted September 26, 2006 Array construct is built such that duplicate keys are removed and the recent occuring one stays.So, $data = array("Joe"=>"Dog", "Joe"=>"Cat");will print Array ( [Joe] => Cat ) What's wrong with returningarray("Joe"=>array("Dog", "Cat", "Rabbit")) ? Link to comment https://forums.phpfreaks.com/topic/22072-array_combine-modification/#findComment-98746 Share on other sites More sharing options...
Barand Posted September 26, 2006 Share Posted September 26, 2006 you could swap he arrays around $c = array_combine ($animals, $names); Link to comment https://forums.phpfreaks.com/topic/22072-array_combine-modification/#findComment-98748 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.