I'm really confused to this logic and seems I can't figure it out, basically what i want is to store count if there is same value in next and next element store it on array then the rest store
This is my code
$json = '[{
"fight_declaration": "1"
},
{
"fight_declaration": "2"
},
{
"fight_declaration": "2"
},
{
"fight_declaration": "1"
},
{
"fight_declaration": "3"
}
]';
$data = json_decode($json,true);
$count = 0;
$array = [];
while ($current = current($data) )
{
$next = next($data);
if (false !== $next && $next['fight_declaration'] == $current['fight_declaration'])
{
$count++;
$array[]['count'] = $count;
}
}
print_r($count);
What i want output is like this
[{
"count": 1
},
{
"count": 2
},
{
"count": 1
},
{
"count": 1
}
]
In my output i want fight_declaration has same value now i want to count it and store it on array
Thanks