3

I would like to order this multi-dimensional array items[] by the key 'rel'.

print_r ($items) will output:

 Array(

 [36] => Array
    (
        [id] => 36
        [name] => mp4
        [total_items] => 58
        [rel] => 5.3015
    )

[61] => Array
    (
        [id] => 61
        [name] => mp3
        [total_items] => 61
        [rel] => 21.7269
    )

[63] => Array
    (
        [id] => 63
        [name] => avi
        [total_items] => 43
        [rel] => 2.254
    )
 )

and I need the rows to be: first [61] second [36] and then [63]

0

4 Answers 4

2

Here is what I use:

function array_sort(&$array, $on, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

So you would use it like this:

array_sort($items, 'rel');
Sign up to request clarification or add additional context in comments.

7 Comments

This should be added to php's standard library considering how often people ask for it here.
@PMV they ask, because they do not read documentation ;) There is a function that allows you to do that without the need for so much code. Using closures it is even more simple.
@Tadek, you can only use anon functions in newer versions of php
@Neal But you can use usort() since PHP4, and PHP5.3 is quite old now (was released in the first half of 2009) - come on, it sure is not something "new"
@Neal Correction: in this case it is about uasort() function.
|
2

There is a function for that and it is called uasort.

See the first and second example from the documentation of usort and adapt it, maybe like that:

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

uasort($your_array, "cmp");

Did it help?

EDIT:

Also, if you have PHP version higher than 5.3, you can use closures:

uasort($your_array, function($a, $b){
    if ($a['rel'] == $b['rel']) {
        return 0;
    }
    return ($a['rel'] < $b['rel']) ? -1 : 1;
});

EDIT2:

I have made a mistake: usort() is used for sorting without maintaining key association (see more info), the uasort() function is what you need. I have corrected examples above.

2 Comments

this is a multidimensional array. How does $a['rel'] exist?
$a and $b are just elements of the array that is sorted. And because these elements are arrays themselves, they also have keys (unless they are empty) - in this example each element of the array has rel key. Is it clear enough?
1
$rel = array();
foreach ( $items as $key => $value )
{
    $rel[$key] = $value['rel'];
}
array_multisort($rel, SORT_ASC, $items);

2 Comments

That basically just sorts the individual arrays, no?
No: "array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions." Reference: us.php.net/array_multisort
0

Try usort(), write your own little callback comparison function.

http://tr.php.net/manual/en/function.uasort.php

http://tr.php.net/manual/en/function.uksort.php

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.