3v4l.org

run code in 300+ PHP versions simultaneously
<?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, ]; } // sort, because why not usort($rows, function ($rowOne, $rowTwo) { return ((int) $rowOne['time']) > ((int) $rowTwo['time']); }); // dump var_dump($rows);
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero in /in/RrNKA on line 50 array(12) { [0]=> array(3) { ["time"]=> string(8) "00:00:00" ["sales"]=> float(55.99) ["orders"]=> int(1) } [1]=> array(3) { ["time"]=> string(8) "02:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [2]=> array(3) { ["time"]=> string(8) "04:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [3]=> array(3) { ["time"]=> string(8) "06:00:00" ["sales"]=> float(46.37) ["orders"]=> int(1) } [4]=> array(3) { ["time"]=> string(8) "08:00:00" ["sales"]=> float(246.56) ["orders"]=> int(4) } [5]=> array(3) { ["time"]=> string(8) "10:00:00" ["sales"]=> float(78.66) ["orders"]=> int(1) } [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 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
Deprecated: usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero in /in/RrNKA on line 52 array(12) { [0]=> array(3) { ["time"]=> string(8) "00:00:00" ["sales"]=> float(55.99) ["orders"]=> int(1) } [1]=> array(3) { ["time"]=> string(8) "02:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [2]=> array(3) { ["time"]=> string(8) "04:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [3]=> array(3) { ["time"]=> string(8) "06:00:00" ["sales"]=> float(46.37) ["orders"]=> int(1) } [4]=> array(3) { ["time"]=> string(8) "08:00:00" ["sales"]=> float(246.56) ["orders"]=> int(4) } [5]=> array(3) { ["time"]=> string(8) "10:00:00" ["sales"]=> float(78.66) ["orders"]=> int(1) } [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.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
array(12) { [0]=> array(3) { ["time"]=> string(8) "00:00:00" ["sales"]=> float(55.99) ["orders"]=> int(1) } [1]=> array(3) { ["time"]=> string(8) "02:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [2]=> array(3) { ["time"]=> string(8) "04:00:00" ["sales"]=> int(0) ["orders"]=> int(0) } [3]=> array(3) { ["time"]=> string(8) "06:00:00" ["sales"]=> float(46.37) ["orders"]=> int(1) } [4]=> array(3) { ["time"]=> string(8) "08:00:00" ["sales"]=> float(246.56) ["orders"]=> int(4) } [5]=> array(3) { ["time"]=> string(8) "10:00:00" ["sales"]=> float(78.66) ["orders"]=> int(1) } [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 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/RrNKA on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/RrNKA on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/RrNKA on line 3
Process exited with code 255.

preferences:
260.35 ms | 401 KiB | 329 Q