-1

I have a php function which builds an array with category data like so:

 $categories = array();
        foreach($catIds as $catId) {
            $category = Mage::getModel('catalog/category')->load($catId);
            $categories[$category->getName()] = array(
                'url' => $category->getUrl(),
                'img' => $category->getImageUrl(),
                'thumb' => Mage::getBaseUrl('media').'catalog' . DS . 'category' . DS.$category->getThumbnail(),
                'position' => $category->getPosition()
            );
        }

I am wanting to sort the $categories array by the 'position' value. I have tried a few variations of ksort and array_multisort but haven't been able to find a solution.

James
  • 3,233
  • 3
  • 40
  • 57

1 Answers1

0

usort — Sort an array by values using a user-defined comparison function

usort($categories, function($a, $b){
    $a = $a['position'];
    $b = $b['position'];
    if($a == $b)
    {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
});

This is assuming that the position is a numeric value and you are sorting ascending.

Scopey
  • 6,269
  • 1
  • 22
  • 34