Using CakePHP 2.7, I have an array of find results:
array(
(int) 0 => array(
'Foo' => array(
'id' => '786',
'bar_id' => '12',
// more data
),
'Bar' => array(
'id' => '12',
// more data
)
),
(int) 1 => array(
'Foo' => array(
'id' => '785',
'bar_id' => '13',
// more data
),
'Bar' => array(
'id' => '13',
// more data
)
),
// many more results
Now I want do have it sorted by 'bar_id' in a defined individual way, not numeric. The sort order is described in another array:
array(9, 3, 18, 36, …)
Is there a better way to do it but to loop over the sort array, pick the current id then loop over the results array (a loop within a loop), look there for the corresponding 'bar_id' and if found write the result into a new results array?
$results = array(…); // the results
$order = array(9, 3, 18, 36, …);
$sortedResults = array();
foreach ($order as $barId) {
foreach ($results as $result) {
if ($barId == $result['Foo']['bar_id']) {
$sortedResults[] = $result;
break;
}
}
}
I know that Cake has the Hash utility (formerly 'Set') to work with arrays, but I couldn't find a way to do it with Has.