3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $rows = [ [ 'time' => '00:00:00', 'sales' => 55.99, 'orders' => 1, ], [ 'time' => '06:00:00', 'sales' => 46.37, 'orders' => 1, ], [ 'time' => '08:00:00', 'sales' => 246.56, 'orders' => 4, ], [ 'time' => '10:00:00', 'sales' => 78.66, 'orders' => 1, ] ]; // isolate the times $times = array_map(function ($row) { return $row['time']; }, $rows); // create a list of similarly formatted times from 00:00:00 - 22:00:00 $allTimes = array_map(function ($hour) { return sprintf('%02d:00:00', $hour); }, range(0, 22, 2)); // compute the difference $notTimes = array_diff($allTimes, $times); // add the notTimes to the original rowset with default values foreach ($notTimes as $notTime) { $rows[] = [ 'time' => $notTime, 'sales' => 0, 'orders' => 0, ]; } var_dump($rows);
Output for 5.4.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(12) { [0]=> array(3) { ["time"]=> string(8) "00:00:00" ["sales"]=> float(55.99) ["orders"]=> int(1) } [1]=> array(3) { ["time"]=> string(8) "06:00:00" ["sales"]=> float(46.37) ["orders"]=> int(1) } [2]=> array(3) { ["time"]=> string(8) "08:00:00" ["sales"]=> float(246.56) ["orders"]=> int(4) } [3]=> array(3) { ["time"]=> string(8) "10:00:00" ["sales"]=> float(78.66) ["orders"]=> int(1) } [4]=> array(3) { ["time"]=> string(8) "02:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [5]=> array(3) { ["time"]=> string(8) "04:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [6]=> array(3) { ["time"]=> string(8) "12:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [7]=> array(3) { ["time"]=> string(8) "14:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [8]=> array(3) { ["time"]=> string(8) "16:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [9]=> array(3) { ["time"]=> string(8) "18:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [10]=> array(3) { ["time"]=> string(8) "20:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [11]=> array(3) { ["time"]=> string(8) "22:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } }
Output for 5.2.3 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/OJgbO on line 3
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.2
<br /> <b>Parse error</b>: syntax error, unexpected '[' in <b>/in/OJgbO</b> on line <b>3</b><br />
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
<br /> <b>Parse error</b>: parse error, unexpected '[' in <b>/in/OJgbO</b> on line <b>3</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br /> <b>Parse error</b>: parse error in <b>/in/OJgbO</b> on line <b>3</b><br />
Process exited with code 255.