3v4l.org

run code in 500+ PHP versions simultaneously
<?php $labels = [ 0 => "2023-07-05", 1 => "2023-07-06", 2 => "2023-07-07", 3 => "2023-07-08", 4 => "2023-07-09", 5 => "2023-07-10", 6 => "2023-07-11", ]; $result = [ 0 => [ "numberViews" => 20, "dayDate" => "2023-07-10", ], 1 => [ "numberViews" => 32, "dayDate" => "2023-07-11", ], ]; // Create the output array; set the dates/labels as keys and 0 as values $output = array_fill_keys($labels, 0); // Fill the number of views for the dated that have views foreach ($result as $day) { $output[$day['dayDate']] = $day['numberViews']; } print_r($output);
Output for 5.4.0 - 5.4.45, 5.5.0, 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
Array ( [2023-07-05] => 0 [2023-07-06] => 0 [2023-07-07] => 0 [2023-07-08] => 0 [2023-07-09] => 0 [2023-07-10] => 20 [2023-07-11] => 32 )
Output for 5.1.0, 5.2.0, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/Ajspl on line 3
Process exited with code 255.
Output for 4.3.0, 4.4.1, 5.0.0
Parse error: parse error, unexpected '[' in /in/Ajspl on line 3
Process exited with code 255.

preferences:
85.08 ms | 1165 KiB | 4 Q