Merge multi-dimensional array preserving keys

The name of the picture


Merge multi-dimensional array preserving keys



So I have an array as follows. I want to merge all of these into one array of key => value.


key => value



Input :


$logTypes = array(
'user_management' => array(
1001 => 'A new user was added',
1002 => '#user_name# accessed user management',
1003 => 'Created',
1004 => 'Edited/Updated'),
'report_management' => array(
2001 => '#user_name# Added a new report',
2002 => '#user_name# viewed "edit history" for the report #report_name#',
),
);



Required Output :


array (
1001 => 'A new user was added',
1002 => '#user_name# accessed user management',
1003 => 'Created',
1004 => 'Edited/Updated',
2001 => '#user_name# Added a new report',
2002 => '#user_name# viewed "edit history" for the report #report_name#',
)



I tried various permutations of array_merge, array_map, array_values but they just don't work as a whole, like some of them manage to merge but re-index the keys etc. etc.


array_merge


array_map


array_values



My attempts:



logTypes = call_user_func('array_merge', array_values(self::$logTypes));


logTypes = call_user_func('array_merge', array_values(self::$logTypes));



$logTypes = call_user_func('array_push', self::$logTypes);


$logTypes = call_user_func('array_push', self::$logTypes);



$logTypes = call_user_func_array('array_merge', self::$logTypes);


$logTypes = call_user_func_array('array_merge', self::$logTypes);



$logTypes = array_values(array_values(self::$logTypes));


$logTypes = array_values(array_values(self::$logTypes));



$array = array_map('current', self::$logTypes);


$array = array_map('current', self::$logTypes);



$merged = array_merge(...self::$logTypes);


$merged = array_merge(...self::$logTypes);



Finally I had to resort to what I was trying to avoid all along, looping through each array :l


$plain_array = array();
foreach (self::$logTypes as $types) {
$plain_array += $types;
}



It works, so now my question is that, Is there any other way to achieve the desired result, using the above array_* functions ?



If I may add, may the shortest solution win!! :)





You could try array_replace function $plain_array = array(); foreach ($logTypes as $types) { $plain_array = array_replace($plain_array, $types); }
– FallDi
5 hours ago


array_replace


$plain_array = array(); foreach ($logTypes as $types) { $plain_array = array_replace($plain_array, $types); }





Thanks for the suggestion, I am looking for not having to loop manually through the original array.
– 9KSoft
5 hours ago





Non loop solution array_reduce( $logTypes, function($carry, $types) { $carry = array_replace($carry, $types); return $carry; }, );
– FallDi
5 hours ago


array_reduce( $logTypes, function($carry, $types) { $carry = array_replace($carry, $types); return $carry; }, );





Thanks @FallDi your solution does work, I even shortened it array_reduce(self::$logTypes, function($carry, $types) { return $carry += $types; }, )
– 9KSoft
5 hours ago


array_reduce(self::$logTypes, function($carry, $types) { return $carry += $types; }, )




3 Answers
3



You can do it like this:


array_reduce($logTypes, function($carry, $item) {
return $carry + $item;
}, );



output:


array (
1001 => 'A new user was added',
1002 => '#user_name# accessed user management',
1003 => 'Created',
1004 => 'Edited/Updated',
2001 => '#user_name# Added a new report',
2002 => '#user_name# viewed "edit history" for the report #report_name#',
)



Source: http://php.net/manual/en/function.array-reduce.php





Nice one! Upvoted.
– 9KSoft
5 hours ago


<?php
$logTypes = array(
'user_management' => array(
1001 => 'A new user was added',
1002 => '#user_name# accessed user management',
1003 => 'Created',
1004 => 'Edited/Updated'),
'report_management' => array(
2001 => '#user_name# Added a new report',
2002 => '#user_name# viewed "edit history" for the report #report_name#',
),
);
$emp_arr = array();
foreach ($logTypes as $data)
{
$Output = array_replace($emp_arr, $data);
}
print_R ($Output);
?>

== Output
Array
(
[1001] => A new user was added
[1002] => #user_name# accessed user management
[1003] => Created
[1004] => Edited/Updated
[2001] => #user_name# Added a new report
[2002] => #user_name# viewed "edit history" for the report #report_name#
)





I didn't downvote, and also I am looking for a no loop solution. Otherwise, my solution of just adding the arrays is working flawlessly. Thanks.
– 9KSoft
5 hours ago



Try this:


$new_array = $logTypes['user_management'] + $logTypes['report_management'];



PHP doc



If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator





My Dear :) , The Input array isn't just limited to those two keys, I just shortened it to keep the question succinct. Thanks.
– 9KSoft
5 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to scale/resize CVPixelBufferRef in objective C, iOS

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

SVG with two text elements. When one resizes due to textLength - how to resize the other one to the same character size