3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Capture the list of taste observations across analyses and phases of analysis $taste_observations = array(); // Temporary test data for Stack Overflow Question $taste_observations[] = [ 'analyst' => 'Ken', 'phase' => 'Entrance', 'taste' => 'Sweet', 'intensity' => 1 ]; $taste_observations[] = [ 'analyst' => 'Ken', 'phase' => 'Entrance', 'taste' => 'Spicy', 'intensity' => 1 ]; $taste_observations[] = [ 'analyst' => 'Ken', 'phase' => 'Center', 'taste' => 'Sweet', 'intensity' => 2 ]; $taste_observations[] = [ 'analyst' => 'Bob', 'phase' => 'Entrance', 'taste' => 'Savory', 'intensity' => 1 ]; $taste_observations[] = [ 'analyst' => 'Bob', 'phase' => 'After', 'taste' => 'Sweet', 'intensity' => 2, ]; $taste_summary = []; foreach($taste_observations as $taste_observation) { $taste_summary[$taste_observation['taste']] = [ 'taste' => $taste_observation['taste'], 'count' => 1 + ($taste_summary[$taste_observation['taste']]['count'] ?? 0), 'max' => max($taste_summary[$taste_observation['taste']]['max'] ?? 0, $taste_observation['intensity']), 'sum' => $taste_observation['intensity'] + ($taste_summary[$taste_observation['taste']]['sum'] ?? 0) ]; } ksort($taste_summary); $taste_summary = array_values($taste_summary); print_r($taste_summary);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Array ( [0] => Array ( [taste] => Savory [count] => 1 [max] => 1 [sum] => 1 ) [1] => Array ( [taste] => Spicy [count] => 1 [max] => 1 [sum] => 1 ) [2] => Array ( [taste] => Sweet [count] => 3 [max] => 2 [sum] => 5 ) )

preferences:
131.88 ms | 408 KiB | 5 Q