spenceddd Posted February 10, 2010 Share Posted February 10, 2010 Hello guys, Say I have two arrays both of which are dynamically created and therefore could change in size - and I want to combine them. Here is an example of two arrays: Array ( [0] => Array ( [0] => 0 ) [100] => Array ( [0] => 1 ) [105] => Array ( [0] => 1 ) [106] => Array ( [0] => 6 ) ) Array ( [0] => Array ( [0] => 0 ) [100] => Array ( [0] => 2 ) [103] => Array ( [0] => 4 ) [104] => Array ( [0] => 1 ) [105] => Array ( [0] => 1 ) ) The result I am looking for is an array which looks for the 'index' eg (100, 103, 104, 105, 106 in this case) and adds the values of their contents together to produce one new array ie: Array ( [100] => Array ( [0] => 3 //this has gone up to 3 from 2 after being combined ) [103] => Array ( [0] => 4 ) [104] => Array ( [0] => 1 ) [105] => Array ( [0] => 2 //this has gone up to 2 from 1 after being combined ) [106] => Array ( [0] => 6 ) ) As you may notice the subarray with the value 0 has been removed, which is another thing I need to do. Im thinking I would need to some how loop through both arrays one after the other allowing both to contribute to the resulting array. Does anyone have any thoughts on the best way to do this...? Thanks a lot for any help. Spencer Link to comment https://forums.phpfreaks.com/topic/191619-combining-multidimensional-arrays/ Share on other sites More sharing options...
GKWelding Posted February 10, 2010 Share Posted February 10, 2010 look at php.net for array_diff_key and array_keys to do what you want. Use array_diff_key to find out the array keys that don't have duplicates, add them to a separate array, unset them as you do this. This way the original arrays now only contain matching array keys. loop through one array and add the values together. The code below works, and I have just tested it too so I'm sure you can modify it to work with multi-dimensional arrays. function combineArrays($array1,$array2) { // this has been added in for testing $array1 = array('0'=>'30','1'=>'10','5'=>'60'); $array2 = array('0'=>'3','2'=>'10','5'=>'6'); // get differences $arr1diffs = array_diff_key($array1,$array2); $arr2diffs = array_diff_key($array2,$array1); // new var $newArray = ''; // add unique keys to new array for both arrays foreach($arr1diffs as $key=>$val) { $newArray[$key] = $val; unset($array1[$key],$key,$val); } foreach($arr2diffs as $key=>$val) { $newArray[$key] = $val; unset($array1[$key],$key,$val); } // sum duplicate keys and add to new array foreach($array1 as $key=>$val) { $newArray[$key] = $val+$array2[$key]; unset($key,$val); } // tidy up unset($array1,$array2,$arr1diffs,$arr2diffs); // remove unwanted 0 element unset($newArray['0']); // return new array return $newArray; } Link to comment https://forums.phpfreaks.com/topic/191619-combining-multidimensional-arrays/#findComment-1010109 Share on other sites More sharing options...
spenceddd Posted February 10, 2010 Author Share Posted February 10, 2010 Great thanks for that. I'm pretty new to php and am just starting to understand what keys are. Much appreciated! Link to comment https://forums.phpfreaks.com/topic/191619-combining-multidimensional-arrays/#findComment-1010113 Share on other sites More sharing options...
GKWelding Posted February 10, 2010 Share Posted February 10, 2010 In fact, I'm feeling so generous here's the modified function for you. function combineArrays($array1,$array2) { // this has been added in for testing $array1 = Array ( '0' => Array ( '0' => 0 ), '100' => Array ( '0' => 1 ), '105' => Array ( '0' => 1 ), '106' => Array ( '0' => 6 ) ); $array2 = Array ( '0' => Array ( '0' => 0 ), '100' => Array ( '0' => 2 ), '103' => Array ( '0' => 4 ), '104' => Array ( '0' => 1 ), '105' => Array ( '0' => 1 ) ); // get differences $arr1diffs = array_diff_key($array1,$array2); $arr2diffs = array_diff_key($array2,$array1); // new var $newArray = ''; // add unique keys to new array for both arrays foreach($arr1diffs as $key=>$val) { $newArray[$key] = $val['0']; unset($array1[$key],$key,$val); } foreach($arr2diffs as $key=>$val) { $newArray[$key] = $val['0']; unset($array1[$key],$key,$val); } // sum duplicate keys and add to new array foreach($array1 as $key=>$val) { $newArray[$key] = $val['0']+$array2[$key]['0']; unset($key,$val); } // tidy up unset($array1,$array2,$arr1diffs,$arr2diffs); // remove unwanted 0 element unset($newArray['0']); // return new array return $newArray; } which outputs... Array ( [106] => 6 [103] => 4 [104] => 1 [100] => 3 [105] => 2 ) I assume this was what you were trying to do? Link to comment https://forums.phpfreaks.com/topic/191619-combining-multidimensional-arrays/#findComment-1010117 Share on other sites More sharing options...
spenceddd Posted February 10, 2010 Author Share Posted February 10, 2010 Oh thanks so much! Yes that is what I was trying to do! Your a star. Link to comment https://forums.phpfreaks.com/topic/191619-combining-multidimensional-arrays/#findComment-1010134 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.