@ 2020-10-12T05:33:02Z <?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);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.33 , 8.2.0 - 8.2.29 , 8.3.0 - 8.3.26 , 8.4.1 - 8.4.13 Array
(
[0] => Array
(
[count] => 1
)
[1] => Array
(
[count] => 2
)
[2] => Array
(
[count] => 1
)
[3] => Array
(
[count] => 1
)
)
Output for 7.2.0 - 7.2.34 Warning: Use of undefined constant JSON_THROW_ON_ERROR - assumed 'JSON_THROW_ON_ERROR' (this will throw an Error in a future version of PHP) in /in/eAjf0 on line 23
Warning: json_decode() expects parameter 4 to be integer, string given in /in/eAjf0 on line 23
Warning: Invalid argument supplied for foreach() in /in/eAjf0 on line 26
Array
(
)
preferences:dark mode live preview ace vim emacs key bindings
125.28 ms | 408 KiB | 5 Q