More efficient way to merge arrays in PHP? -
here's i'm doing:
$daterange = array('2010-01-01', '2010-01-02', '2010-01-03', ...); $data = array('2010-01-01' => 5, '2010-01-03' => 7); foreach ($daterange $date) { $values[$date] = $data[$date]; }
$values
results in:
array ( '2010-01-01' => 5 '2010-01-02' => null '2010-01-03' => 7 ... )
seems efficient, wonder if there's simple php function that'll handle this.
$values = array_merge(array_fill_keys($daterange, null), $data);
whether that's more efficient can found out through testing, i'd it's more concise, easy understand, , it doesn't throw errors non-existent keys in $data
. :)
Comments
Post a Comment