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.

How to sort this array by pos attribute even though keys (name, store_id, product etc.)

[Attributes] => Array
(
    [name] => Array
        (
            [pos] => 30
        )

    [store_id] => Array
        (
            [pos] => 10
        )

    [product] => Array
        (
            [pos] => 20
        )

)

Edit: performance is important of course.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

You could use uasort() which lets you define your sorting logic and also maintains your associative indexes. Please note that it changes your original array and only returns a boolean based on success.

uasort($your_array, function($a, $b) {
    return $a['pos'] > $b['pos'];
});

My example works >= PHP 5.3 , but for older versions you can use a normal compare function as well.

See uasort() Documentation for details.

share|improve this answer
    
can you check your code with this sample (it is almost the same with above structure): $attr['Attr']['name']['pos'] = 30; $attr['Attr']['price']['pos'] = 10; $attr['Attr']['desc']['pos'] = 20; –  dino May 9 '11 at 13:05
1  
@dino You could have provided the exact specs you need in your question :). What do you want to sort here? You could try inserting $attr['Attr'] in place of $your_array. –  kapa May 9 '11 at 13:11
    
@baz.. - Sweet Jesus. sometimes my brain doesn't work :) It works well. thanks a loot –  dino May 9 '11 at 13:14
1  
@dino Happy to help :). –  kapa May 9 '11 at 13:17

Have a look at the PHP function array_multisort.

http://php.net/manual/en/function.array-multisort.php

There is an example-function in the comments, which should be fine for you:

function array_orderby()
{
    $args = func_get_args();
    $data = array_shift($args);
    foreach ($args as $n => $field) {
        if (is_string($field)) {
            $tmp = array();
            foreach ($data as $key => $row)
                $tmp[$key] = $row[$field];
            $args[$n] = $tmp;
            }
    }
    $args[] = &$data;
    call_user_func_array('array_multisort', $args);
    return array_pop($args);
}

// $dataArray is the array that contains your data
$sorted = array_orderby($dataArray, 'pos', SORT_DESC);
share|improve this answer
    
@Jung - I didn't do it actually... consider there are a couple of more keys and like these structure that I need to order. So how can I manage it with array_multisort? Should I change structure? or better idea? –  dino May 9 '11 at 13:02
1  
Actually its not as simple as I though first. But looking at the linked page you find an example in the comments, which I just added to my answer. This should sort $dataArray by 'pos' descending –  JochenJung May 9 '11 at 13:10
    
@Jung - somethings wrong with your example. Actually i didn't sort it even though I provided the exact specs using your function. –  dino May 9 '11 at 13:18
1  
Looks like a bit overkill for this one. –  kapa May 9 '11 at 13:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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