How can i increase the seqnr if the id is the same like example below.
ID SeqNr
111 1
111 1
111 1
222 2
222 2
333 3
555 4
555 4
I have an array with repeated id so if the id's are the same i want the seqNr to be 1 and if the id's are different then the seqnr should increment.
<?php
$array = [
['id' => 111, 'name' => 'one'],
['id' => 111, 'name' => 'one'],
['id' => 111, 'name' => 'one'],
['id' => 222, 'name' => 'two'],
['id' => 333, 'name' => 'three'],
['id' => 444, 'name' => 'four'],
['id' => 444, 'name' => 'four'],
];
foreach ($array as $key => $value) {
$id = $value['id'];
$seqNr = 1;
echo " ID: " . $id . " SeqNr " . $seqNr;
}
?>
I would really appreciate your help. Thank you!
<?php
$array = [
['id' => 111, 'name' => 'one'],
['id' => 111, 'name' => 'one'],
['id' => 111, 'name' => 'one'],
['id' => 222, 'name' => 'two'],
['id' => 333, 'name' => 'three'],
['id' => 444, 'name' => 'four'],
['id' => 444, 'name' => 'four'],
];
$seqNr = 0;
$lastid= 0;
foreach ($array as $key => $value) {
$id = $value['id'];
if($lastid!==$value['id']){
$seqNr++;
}
$lastid=$value['id'];
echo "<pre>";
echo " ID: " . $id . " SeqNr " . $seqNr;
echo "</pre>";
}
?>
ID: 111 SeqNr 2
ID: 111 SeqNr 2
ID: 111 SeqNr 2
ID: 222 SeqNr 3
ID: 333 SeqNr 4
ID: 444 SeqNr 5
ID: 444 SeqNr 5