OsvaldoM Posted April 13, 2010 Share Posted April 13, 2010 Hello, i've been reading the php manual and googling around with no luck so far. Basically what i want to do is to merge two arrays the following way: $array1 = array("dog" => "brown", "cat" => "white"); $array2 = array("dog" => "black", "duck" => "white") ; and have this as a result: $array3 = array("dog" => array("brown", "black"), "cat" => "white", "duck" => "white") ; I know it's possible and probably quite simple, but my tests so far have failed miserable, basically i get stuck trying to push "black" into the array, whenever "dog" is repeated. Any pointers or suggestions would be quite appreciated! Link to comment https://forums.phpfreaks.com/topic/198440-merging-arrays-question/ Share on other sites More sharing options...
jcbones Posted April 13, 2010 Share Posted April 13, 2010 A simple work around would be. <?php $array1 = array("dog" => "brown", "cat" => "white"); $array2 = array("dog" => "black", "duck" => "white") ; foreach($array1 as $key => $value) { $array3[$key][] = $value; } foreach($array2 as $key => $value) { $array3[$key][] = $value; } echo '<pre>'; print_r($array3); echo '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/198440-merging-arrays-question/#findComment-1041301 Share on other sites More sharing options...
teamatomic Posted April 13, 2010 Share Posted April 13, 2010 $new_array=array_merge_recursive($array1,$array2); HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/198440-merging-arrays-question/#findComment-1041320 Share on other sites More sharing options...
OsvaldoM Posted April 14, 2010 Author Share Posted April 14, 2010 you guys rock, thanks for this... somehow i skipped the array_merge_recursive function in the manual. As i've been dealing with a lot of arrays lately, i've noticed that foreach is not the best way to deal with large arrays, built-in php functions for arrays should be the way to go, sadly, im still not that familiar with all of them... Link to comment https://forums.phpfreaks.com/topic/198440-merging-arrays-question/#findComment-1041600 Share on other sites More sharing options...
jcbones Posted April 14, 2010 Share Posted April 14, 2010 Don't feel bad, I've looked at that manual until my eyes bleed, and I still missed it. Link to comment https://forums.phpfreaks.com/topic/198440-merging-arrays-question/#findComment-1041976 Share on other sites More sharing options...
teamatomic Posted April 14, 2010 Share Posted April 14, 2010 @OsvaldoM If you want to work with each item or each nested array foreach or while>each ~is~ the best way. But both are inefficient if you want to find one item or see if the array contains something, then array_search or array_key_exists is the way to go. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/198440-merging-arrays-question/#findComment-1042023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.