Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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.

share|improve this question

marked as duplicate by Ja͢ck, HamZa, Alma Do, bodi0, Lashane Apr 24 at 6:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.