I have an array that looks like this:
$myArray(
'totals' => array(
Entity\Total,
Entity\Total
...
)
'subtotals' => array(
Entity\Total,
Entity\Total
...
)
)
I'm wondering how to sort totals and subtotals arrays of objects based on field from object of class Total.
I tried something with usort and stuff with no luck. This returns duplicated arrays of unsorted objects.
uasort($myArray, function($a, $b){
foreach($a as $adata) {
foreach($b as $bdata){
if ($adata->getmaxTotal() > $bdata->getmaxTotal())
return 1;
else
return 0;
}
}
});
array_multisort could sort several arrays at the same time. But has no analog with callback. So you need to create new array, that could be ordered with this function.
for ($i = 0; $i < 10; $i++)
{
$a[$i] = new stdClass();
$a[$i]->id = $i;
$a[$i]->property = $i % 4;
}
for ($i = 0; $i < 10; $i++)
{
$b[$i] = new stdClass();
$b[$i]->id = $i;
$b[$i]->property = $i % 3;
}
// creating temporary array
$c = array_map(function ($item)
{ return $item->property; }, $a);
array_multisort($c, $a, $b);
var_dump($a, $b);