Skip to content

Instantly share code, notes, and snippets.

@SKoschnicke
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SKoschnicke/9367391 to your computer and use it in GitHub Desktop.
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.
<?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