Values of array are grouped together if they have the same $record->parent_id. How can I assign to each group a key that starts from 0 and iterates for every member of a group and only for that specific group? Every consecutive new group starts from 0 again.
Example: if $record->parent_id is 2, current iteration of foreach loop assigns a 0 to key. Next iteration has $record->parent_id of value 56, its key is 0 again! Next iteration has $record->parent_id of value 2 again, hence its key is now 1 and so forth.
My current code:
<?php foreach ($records as $key => $record): ?>
<tr id="<?php echo $key; ?>">
<td><?php echo $record->parent_id; ?></td>
<td><?php echo $key; ?></td> //here I wish to get key starting from 0
<td><?php echo $record->title; ?></td>
</tr>
<?php endforeach; ?>