Jump to content

Combining multidimensional arrays


spenceddd

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.