How do I sort a PHP array by an element nested inside? [duplicate]
Asked Answered
P

7

47

I have an array like the following:

Array
(
    [0] => Array
        (
            'name' => "Friday"
            'weight' => 6
        )
    [1] => Array
        (
            'name' => "Monday"
            'weight' => 2
        )
)

I would like to grab the last values in that array (the 'weight'), and use that to sort the main array elements. So, in this array, I'd want to sort it so the 'Monday' element appears before the 'Friday' element.

Panocha answered 13/9, 2010 at 15:27 Comment(1)
Yes, this looks to be the case. I looked at that issue before this, but didn't quite understand it until reading through the replies to this thread... ah well :(Panocha
I
47

You can use usort as:

function cmp($a, $b) {
   return $a['weight'] - $b['weight'];
}

usort($arr,"cmp");
Intercollegiate answered 13/9, 2010 at 15:29 Comment(2)
You can simply use return $a['weight'] - $b['weight'];.Elspeth
This didn't work perfectly for me but this seems total correct solution:Genitourinary
C
9

Can be done using an anonymous function.

Also if your 'weight' is a string use one of the other returns (see the commented out lines):

<?php

$arr = array(
    0 => array (
        'name'   => 'Friday',
        'weight' => 6,
    ),
    1 => array (
        'name'   => 'Monday',
        'weight' => 2,
    ),
);

// sort by 'weight'
usort($arr, function($a, $b) { // anonymous function
    // compare numbers only
    return $a['weight'] - $b['weight'];

    // compare numbers or strings
    //return strcmp($a['weight'], $b['weight']);

    // compare numbers or strings non-case-sensitive
    //return strcmp(strtoupper($a['weight']), strtoupper($b['weight']));
});

var_export($arr);

/*
array (
    0 => array (
        'name'   => 'Monday',
        'weight' => 2,
    ),
    1 => array (
        'name'   => 'Friday',
        'weight' => 6,
    ),
)
*/
Collazo answered 18/4, 2014 at 17:49 Comment(0)
S
9

You can also use an anonymous function.

usort($items, function($a, $b) {
    return $a['name'] > $b['name'];
});
Stereotomy answered 13/11, 2014 at 16:13 Comment(0)
P
3

Agree with usort, I also sometimes use array_multisort (https://www.php.net/manual/en/function.usort.php) example 3, sorting database results. You could do something like:

<?php
$days = array(
  array('name' => 'Friday', 'weight' => 6),
  array('name' => 'Monday', 'weight' => 2),
);

$weight = array();
foreach($days as $k => $d) {
  $weight[$k] = $d['weight'];
}

print_r($days);

array_multisort($weight, SORT_ASC, $days);

print_r($days);
?>

Output:

Array
(
    [0] => Array
        (
            [name] => Friday
            [weight] => 6
        )

    [1] => Array
        (
            [name] => Monday
            [weight] => 2
        )

)
Array
(
    [0] => Array
        (
            [name] => Monday
            [weight] => 2
        )

    [1] => Array
        (
            [name] => Friday
            [weight] => 6
        )

)
Pythagorean answered 13/9, 2010 at 15:46 Comment(0)
S
2

If the filed you sort by is string like title name,
array_multisort + Flags for Natural Sorting and CaseInSensitivity are the way to go:

$sort_by_title = array();
foreach($items as $item) {
  $sort_by_title [] = $item['title'];
}
array_multisort($sort_by_title , SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE, $items );
Swashbuckler answered 26/7, 2016 at 14:31 Comment(0)
B
0

NOTE, if the element you are sorting on is a float like .0534 and .0353 (like for a percentage), then you have to multiply both by 1000. not sure why frankly... it appears that usort seems to compare the integer values.

took me awhile to figure that one out...

and 2 tips that may not be immediately obvious:

  1. if your arrays are objects, you can use return $a->weight - $b->weight of course
  2. if you return $b->weight - $a->weight, it will sort desending.
Barris answered 17/8, 2017 at 1:18 Comment(0)
P
-1

Here's a cool function that might help:

function subval_sort($a,$subkey,$sort) {
    foreach($a as $k=>$v) {
        $b[$k] = strtolower($v[$subkey]);
    }
    if($b)
    {
        $sort($b);
        foreach($b as $key=>$val) {
            $c[] = $a[$key];
        }
        return $c;
    }
}

Send in the array as $a the key as $subkey and 'asort' or 'sort' for the $sort variable

Peoria answered 13/9, 2010 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.