Here is a snippet of my array that is used in php 5.3.x :
$arr[$category][$item][$attr_qty] = 3;
$arr[$category][$item][$attr_price] = 12.00;
$category
are arbitrary integers, as are $item
, as are $attr_qty
and $attr_price
.
Is there a quick way of sorting, by $attr_qty
, the items in each category?
Using integers makes the code easier, but I get the feeling I will have to use associative arrays.
You can use usort
which allows you to specify a custom sorting function
usort($arr, 'customSortFunction');
function customSortFunction($a, $b)
{
if ($a['item']['attr_qty'] > $b['item']['attr_qty']) return 1; //First element is bigger
elseif ($a['item']['attr_qty'] < $b['item']['attr_qty']) return -1; //Second element is bigger
else return 0; //Both are equal
}