Skip to content

Instantly share code, notes, and snippets.

View SKoschnicke's full-sized avatar

Sven Koschnicke SKoschnicke

View GitHub Profile
@SKoschnicke
SKoschnicke / merge.php
Last active August 29, 2015 13:57
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);
}