PHP - Merge two arrays on same key - php

I'd like to merge two arrays on same key.
Here's the 1st array :
Array
(
[2052] => Array
(
[495] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 79755
)
[1] => Array
(
[ID_RI_BELANJA] => 79755
)
)
)
[4566] => Array
(
[488] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 231610
)
[1] => Array
(
[ID_RI_BELANJA] => 231610
)
)
)
)
And this is the 2nd array
Array
(
[2052] => Array
(
[495] => Array
(
[TOTAL_RI] => 1000000
[TOTAL_ANGGARAN] => 500000
)
)
[4566] => Array
(
[488] => Array
(
[TOTAL_RI] => 2000000
[TOTAL_ANGGARAN] => 1000000
)
)
)
And i'd like merge that two arrays to be like this :
Array
(
[2052] => Array
(
[495] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 79755
)
[1] => Array
(
[ID_RI_BELANJA] => 79755
)
[TOTAL_RI] => 1000000
[TOTAL_ANGGARAN] => 500000
)
)
[4566] => Array
(
[488] => Array
(
[0] => Array
(
[ID_RI_BELANJA] => 231610
)
[1] => Array
(
[ID_RI_BELANJA] => 231610
)
[TOTAL_RI] => 2000000
[TOTAL_ANGGARAN] => 1000000
)
)
)
This is my first project and I don't know what to do.
Can anyone tell me how to do that?
Pls

If your arrays have same Key then:
$array1 = array(); //put your value in this array
$array2 = array(); //put your value in this array
$array3 = array();
$array3[] = $array1;
$array3[] = $array2;

Assuming your two arrays are $array1 and $array 2 respectively, try this:
foreach($array1 as $k1 => $v1) {
foreach($v1 as $k2 => $v2) {
foreach($v2 as $k3 => $v3) {
$new[$k1][$k2][$k3] = $array1[$k1][$k2][$k3];
$new[$k1][$k2] = array_merge($new[$k1][$k2], $array2[$k1][$k2]);
}
}
}

=> store the all value in this variable
$Arr1 //put your value in this array
$Arr2 //put your value in this array
=> And merge it
$ResponseDetails = array_merge( (array)$Arr1, (array)$Arr2);

array_replace_recursive should do the job:
// $arr1 is the 1st array, $arr2 - is your 2nd array
$result = array_replace_recursive($arr1, $arr2); // now the $result variable contains the expected merged result
http://php.net/manual/en/function.array-replace-recursive.php

Related

Sum values in an multi-dimensional array

I want to sum the values of the following array
I would like to group the same items by cumulating them, the output format would also be an array
Array
(
[0] => Array
(
[0] => Array
(
[0] => R421_FD03
[1] => 1
)
[1] => Array
(
[0] => R421_FD03
[1] => 1
)
)
[1] => Array
(
[0] => Array
(
[0] => R421_FD03
[1] => 1
)
[1] => Array
(
[0] => R421_FD02
[1] => 1
)
)
)
I tested this code but the result is not the one I'm waiting for:
$sumArray = array();
array_walk_recursive($data, function($item, $key) use (&$sumArray){
$sumArray[$key] = isset($sumArray[$key]) ? $item + $sumArray[$key] : $item;
});
The result I'm waiting for is this one
Array
(
[0] => Array
(
[0] => R421_FD03
[1] => 3
)
[1] => Array
(
[0] => R421_FD02
[1] => 1
)
)
)
You can apply simple foreach() with array_values()
$final_array = [];
foreach($array as $arr){
foreach($arr as $ar){
$final_array[$ar[0]][0] = $ar[0];
$final_array[$ar[0]][1] = (isset($final_array[$ar[0]][1])) ? $final_array[$ar[0]][1] + $ar[1] : $ar[1];
}
}
$final_array = array_values($final_array);
print_r($final_array);
Output:-https://3v4l.org/lABWG
You can use array_merge with splat operator and then iterate through the array
$merged = array_merge(...$arr);//splat operator
$r = [];
array_walk($merged, function($v,$k) use(&$r){
array_key_exists($v['0'], $r) ? ($r[$v[0]] += $v['1']) : ($r[$v[0]] = $v[1]);
});
print_r($r);
Result :-
Array
(
[R421_FD03] => 3
[R421_FD02] => 1
)

How can i rearrange a php array without using key names

My array is below i need to arrange like array2 (without use $aa['caption1'] like names directly)
arrray1 is
Array
(
[0] => Array
(
[caption1] => Array
(
[0] => gfdhgfjhg
[1] => dfhfgjghk
)
[caption2] => Array
(
[0] => shgjgh
[1] => dhfgkgjl
)
[banner_image] => Array
(
[0] => assets/images/page_content/img_namT7.jpg
[1] => assets/images/page_content/img_R8mzP.jpg
)
)
[1] => Array
(
[heading] => Array
(
[0] =>
)
[pragraph] => Array
(
[0] =>
)
)
)
arrray2 is(Required format )
Array
(
[0] => Array
(
array('caption1'=>'caption1','caption2'=>'shgjgh','banner_image'=>'assets/images/page_content/img_namT7.jpg'),
array('caption1'=>'dfhfgjghk','caption2'=>'dhfgkgjl','banner_image'=>'page_content/img_R8mzP.jpg')
)
[1] => Array
(
array('heading'=>'','pragraph'=>''),
array('heading'=>'fgh','pragraph'=>'ghgh'),
)
)
please any one help me.
The solution using array_keys, array_map and array_combine functions:
// $arr is your initial array
$result = [];
foreach($arr as $v){
$keys = array_keys($v);
$data = call_user_func_array('array_map', [null] + $v);
$result[] = array_map(function($item) use($keys){
return array_combine($keys, $item);
}, $data);
}
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Array
(
[caption1] => gfdhgfjhg
[caption2] => shgjgh
[banner_image] => assets/images/page_content/img_namT7.jpg
)
[1] => Array
(
[caption1] => dfhfgjghk
[caption2] => dhfgkgjl
[banner_image] => assets/images/page_content/img_R8mzP.jpg
)
)
[1] => Array
(
[0] => Array
(
[heading] =>
[pragraph] =>
)
)
)

Assigning an array as a value of another array

I have three arrays i need to create an array which can be multidimensional.
1st array
Array
(
[0] => Test_One
[1] => Test_two
)
2nd array
Array
(
[0] => www.link.com
[1] => www.link2.com
)
3rd array
Array
(
[0] => Song1
[1] => song2
)
What i want
Array
(
[www.link.com] => Array
(
[0] => Test_one
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
[1] => Song2
)
)
Assuming that you have same number of elements in all three arrays:
<?php
$arr1 = Array
(
0 => "Test_One",
1 => "Test_two"
);
$arr2 = Array
(
0 => "www.link.com",
1 => "www.link2.com"
);
$arr3 = Array
(
0 => "Song1",
1 => "Song2"
);
$final = []; //for versions below PHP 5.4 use $final = array();
foreach($arr2 as $key=>$value) {
$final[$value] = [$arr1[$key],$arr3[$key]];
}
print_r($final);
will output:
Array
(
[www.link.com] => Array
(
[0] => Test_One
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
[1] => Song2
)
)
Update: Simplified foreach loop. From Comments #uchiha
Assuming that you havn't same number of elements in all three arrays:
<?php
$arr1 = Array
(
0 => "Test_One",
1 => "Test_two"
);
$arr2 = Array
(
0 => "www.link.com",
1 => "www.link2.com"
);
$arr3 = Array
(
0 => "Song1",
);
$final = []; //for versions below PHP 5.4 use $final = array();
foreach($arr2 as $key=>$value) {
if(array_key_exists($key,$arr1)) {
$final[$value][] = $arr1[$key];
}
if(array_key_exists($key,$arr3)) {
$final[$value][] = $arr3[$key];
}
}
print_r($final);
Output:
Array
(
[www.link.com] => Array
(
[0] => Test_One
[1] => Song1
)
[www.link2.com] => Array
(
[0] => Test_two
)
)
Hope this help :)
<?php
$array1 = Array
(
'Test_One',
'Test_two'
);
$array2 = Array
(
'www.link.com',
'www.link2.com'
);
$array3 = Array
(
'Song1',
'song2'
);
$array4 = array();
$i = 0;
foreach ($array2 as $a2){
$array4[$a2][] = $array1[$i];
$array4[$a2][] = $array3[$i];
$i++;
}
echo "<pre>";
print_r($array4);
?>

Multidimentional Associative array with array values

i wanted to covert array values into a multidimensional associative array.
Array size can be dynamic.
Array to be converted:
Array
(
[0] => abc
[1] => 0
[2] => xyz
[3] => 0
)
Expected Output:
Array
(
[abc] => Array
(
[0] => Array
(
[xyz] => Array
(
[0] =>
)
)
)
)
Tried with popping first key, but that has no luck...
Try this..
$tmpArr = array('Abc', '0', 'ABC','0');
$array = array();
foreach (array_reverse($tmpArr) as $arr)
$array = array($arr => $array);

How to merge Arrays having same keys only and neglect different keys?

I am new in PHP and love to learn it.
I want to merge two or more arrays having same keys only. And neglect the arrays whose keys are not in both the Arrays. Like
Here is the First Array :
Array
(
[1] => Array
(
[111] => 36265
)
[2] => Array
(
[222] => 36265
)
[3] => Array
(
[333] => 36265
)
)
and Second Array as :
Array
(
[1] => Array
(
[444] => 36265
)
[2] => Array
(
[555] => 36265
)
[4] => Array
(
[666] => 36265
)
)
And i want my result to be as :
Array
(
[1] => Array
(
[111] => 36265
[444] => 36265
)
[2] => Array
(
[222] => 36265
[555] => 36265
)
)
Neglecting the rest Array with key [3] & [4]....
So Please anyone tell me how to get this. I try out "array_merge_recursive()" but this one displays all keys.
Thanks in advance.
You'd have to loop over one of the arrays, check for the existence of the current key in the other array, if it exists, merge them, eg:
$output = array();
foreach ($array1 as $key => $value) {
if (array_key_exists($key, $array2)) {
$output[$key] = $value + $array2[$key];
}
}
Here's a demo
You're probably looking for array_intersect_key.
You could also use a foreacah loop and create a new one. While in this loop you can use one of the arrays keys to match them both. Consider this example:
$array1 = array( 1 => array(111 => 36265), 2 => array(222 => 36265), 3 => array(333 => 36265), ); $array2 = array( 1 => array(444 => 36265), 2 => array(555 => 36265), 4 => array(666 => 36265), );
$new_array = array();
foreach($array1 as $key => $value) {
if(isset($array2[$key])) {
$new_array[$key][key($array1[$key])] = reset($array1[$key]);
$new_array[$key][key($array2[$key])] = reset($array2[$key]);
}
}
echo '<pre>';
print_r($new_array);
echo '</pre>';
Should yield something like this:
Array
(
[1] => Array
(
[111] => 36265
[444] => 36265
)
[2] => Array
(
[222] => 36265
[555] => 36265
)
)

Categories