cbuckley
-
PHP,
pasted
on Jan 10:
|
<?php
$input = array(
array('2011-01-01', 'a', '1'),
array('2011-01-01', 'c', '5'),
array('2011-01-01', 'd', '3'),
array('2012-01-01', 'a', '1')
);
// used to populate the default strings (a b c d)
$strings = range('a', 'd');
$stringDefaults = array_combine($strings, array_fill(0, count($strings), '0'));
$output = array();
foreach ($input as $row) {
list ($date, $string, $number) = $row;
if (!isset($output[$date])) {
$output[$date] = $stringDefaults;
}
$output[$date][$string] = $number;
}
echo json_encode($output);
|
Output:
|
{"2011-01-01":{"a":"1","b":"0","c":"5","d":"3"},"2012-01-01":{"a":"1","b":"0","c":"0","d":"0"}}
|
|