![Creative 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!! :)
array_replace
$plain_array = array(); foreach ($logTypes as $types) { $plain_array = array_replace($plain_array, $types); }
array_reduce( $logTypes, function($carry, $types) { $carry = array_replace($carry, $types); return $carry; }, );
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
<?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#
)
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
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.
You could try
array_replacefunction$plain_array = array(); foreach ($logTypes as $types) { $plain_array = array_replace($plain_array, $types); }– FallDi
5 hours ago