Last active
August 29, 2015 13:57
-
-
Save SKoschnicke/9367391 to your computer and use it in GitHub Desktop.
Merge an array into an array of arrays, appending values to the array if the key exists or creating a new array with the value if not.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$a = array(1 => array(1),2 => array(2),3 => array(3), 12 => array(5)); | |
$b = array(1 => 5,2 => 6,3 => 7, 5 => 12); | |
$result = $a + array_fill_keys(array_keys($b), []); | |
foreach ($b as $key => $value) { | |
array_push($result[$key], $value); | |
} | |
print_r($result); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment