What is the best way to add an Item x of total to each item while sorting an array in php

Solution:
You can try something like that:
// Get array of all order ids.
$orderIds = array_column($array, 'order_id');
// Sort $array by order ids.
array_multisort(
array_column($array, 'driver_id'),
array_column($array, 'product_id'),
$orderIds,
$array
);
// Count order ids occurances.
$orderIdsCounts = array_count_values($orderIds);
$counter = 0;
$array = array_map(function ($orderId, $item) use (&$counter, $orderIdsCounts) {
$counter += 1;
$item['sequence'] = "$counter of {$orderIdsCounts[$orderId]}";
// Reset the counter if order id total is reached.
$counter = $counter === $orderIdsCounts[$orderId] ? 0 : $counter;
return $item;
}, $orderIds, $array);
Here is working demo.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: closed without sending a request; it was probably just an unused speculative preconnection
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.