dadamssg87 Posted October 10, 2011 Share Posted October 10, 2011 I have two arrays, both with the same key values. I'd like to combine them. So for instance... <?php $array1['abcd'] = array( 'value1' => "blah", 'value2' => "blahblah"); $array1['efgh'] = array( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz"); $array2['abcd'] = array('value3' => "three", 'value4' => "four"); $array2['efgh'] = array( 'value3' => "hohoho", 'value6' => "six6"); function combine_arrays($array1,$array2) { //*combining* return $single_array; } echo "<pre>"; print_r(combine_arrays($array1,$array2)); echo "</pre>"; /* would produce ['abcd'] = ( 'value1' => "blah", 'value2' => "blahblah", 'value3' => "three", 'value4' => "four" ) ['efgh'] = ( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz", 'value3' => "hohoho", 'value6' => "six6" ) */ ?> What's the easiest way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/248850-combing-arrays-within-arrays/ Share on other sites More sharing options...
requinix Posted October 10, 2011 Share Posted October 10, 2011 array_merge_recursive? Quote Link to comment https://forums.phpfreaks.com/topic/248850-combing-arrays-within-arrays/#findComment-1277958 Share on other sites More sharing options...
silkfire Posted October 10, 2011 Share Posted October 10, 2011 There are many ways to do this, depending if I understood you right. How do you want the resulting array to look like? Do you want to flatten the array? Or do you want to consolidate the array? Quote Link to comment https://forums.phpfreaks.com/topic/248850-combing-arrays-within-arrays/#findComment-1277959 Share on other sites More sharing options...
dadamssg87 Posted October 10, 2011 Author Share Posted October 10, 2011 array_merge_recursive was exactly what i was looking for. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/248850-combing-arrays-within-arrays/#findComment-1277961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.