3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = <<<JSON [ { "fight_declaration": "1" }, { "fight_declaration": "2" }, { "fight_declaration": "2" }, { "fight_declaration": "1" }, { "fight_declaration": "3" } ] JSON; $data = json_decode($json, false, 512, JSON_THROW_ON_ERROR); $counts = []; foreach ($data as $entry) { if (!isset($previous)) { $currentCount = ['count' => 1]; } elseif ($entry->fight_declaration === $previous->fight_declaration) { $currentCount['count']++; } else { $counts[] = $currentCount; $currentCount = ['count' => 1]; } $previous = $entry; } if (isset($currentCount)) { $counts[] = $currentCount; } print_r($counts);

preferences:
24.74 ms | 405 KiB | 5 Q