- json_decode: documentation ( source)
- print_r: documentation ( source)
<?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);