- var_dump: documentation ( source)
- asort: documentation ( source)
- arsort: documentation ( source)
<?php
$aO = new ArrayObject();
$aO['3'] = (object)['sortKey'=>8, 'label'=>'three'];
$aO['1'] = (object)['sortKey'=>6, 'label'=>'one'];
$aO['a'] = (object)['sortKey'=>4, 'label'=>'Letter A'];
$output = applyOrder($aO, ['sortKey'=>'ASC']);
var_dump($output);
function applyOrder(array $resources, array $orderBy)
{
$results = $resources;
foreach ($orderBy as $property => $order) {
$sortable = [];
foreach ($results as $key => $object) {
$sortable[$key] = $object->$property;
if (null === $sortable[$key]) {
unset($sortable[$key]);
}
}
if ('ASC' === $order) {
asort($sortable);
} elseif ('DESC' === $order) {
arsort($sortable);
}
$results = [];
foreach ($sortable as $key => $propertyValue) {
$results[$key] = $resources[$key];
}
}
return $results;
}