Sort php sub array based on sub key -


here snippet of array used in php 5.3.x :

$arr[$category][$item][$attr_qty] = 3; $arr[$category][$item][$attr_price] = 12.00; 

$category arbitrary integers, $item, $attr_qty , $attr_price.

is there quick way of sorting, $attr_qty, items in each category?

using integers makes code easier, feeling have use associative arrays.

you can use usort allows specify custom sorting function

usort($arr, 'customsortfunction');  function customsortfunction($a, $b) {     if     ($a['item']['attr_qty'] > $b['item']['attr_qty']) return 1;  //first element bigger     elseif ($a['item']['attr_qty'] < $b['item']['attr_qty']) return -1; //second element bigger     else return 0;  //both equal } 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -