I wondering whether anyone has any good ideas on optimizing the following code. I have an multi-dimensional array ($List) as follows:
Array
(
[0] => Array
(
[id] => 1
[title] => A good read
[priority] => 10
)
[1] => Array
(
[id] => 2
[title] => A bad read
[priority] => 20
)
[2] => Array
(
[id] => 3
[title] => A good read
[priority] => 10
)
)
First I'm removing any entries that share the same title (no matter what the other values are) as follows:
$List_new = array();
foreach ($List as $val) {
$List_new[$val['title']] = $val;
}
$List = array_values($List_new);
Perfect. Then I'm reordering the array, first by the priority field and then id:
$sort_id = array();
$sort_priority = array();
foreach ($List as $key => $row) {
$sort_id[$key] = $row['id'];
$sort_priority[$key] = $row['priority'];
}
array_multisort($sort_priority, SORT_DESC, $sort_id, SORT_DESC, $List);
Both code blocks appear in a loop, hence the clearing of $sort_id and $sort_priority before reordering.
Is there a better way to do this - i.e. use the sorting process to remove duplicate title entries? This code block is being executed in a loop of up to 500,000 records and so any improvement would be welcome!