5

Okay, I have an array that is used to transport names, it looks like this:

array(2) {
  [0]=>
  array(3) {
    ["firstName"]=>
    string(3) "Joe"
    ["lastName"]=>
    string(5) "Black"
    ["uid"]=>
    int(3225)
  }
  [1]=>
  array(3) {
    ["firstName"]=>
    string(4) "John"
    ["lastName"]=>
    string(3) "Doe"
    ["uid"]=>
    int(3516)
  }
}

Now, how do I sort this array by lastName?

nkcmr
  • 10,690
  • 25
  • 63
  • 84
  • 9
    While the related questions might not be a 1:1 match for your circumstances, the majority of them contain the function and explanation necessary to complete your task. – Mike B Nov 02 '11 at 16:29
  • 2
    possible duplicate of [Sort a multi-dimensional array](http://stackoverflow.com/questions/648405/sort-a-multi-dimensional-array) – Naftali Nov 02 '11 at 16:32
  • 2
    Answered almost exactly (key needs changed) at http://stackoverflow.com/questions/3596011/sort-an-array-base-on-key – jprofitt Nov 02 '11 at 16:40

3 Answers3

17

StackOverflow has lots of similar questions, but let me give you a quick example. For this, you can use the usort() function.

PHP 5.3 example (not the nicest one, but might be easier to understand):

uasort($array, function ($i, $j) {
    $a = $i['lastName'];
    $b = $j['lastName'];
    if ($a == $b) return 0;
    elseif ($a > $b) return 1;
    else return -1;
});
kapa
  • 77,694
  • 21
  • 158
  • 175
  • Is all the 'if.. else' necessary? I think this could be refactored to simply `return $i['lastName'] > $j['lastName'];` – galdikas Feb 22 '16 at 15:18
  • @galdikas The doc says the function should return 1, 0 or -1. I havent touched PHP for a very long time though. – kapa Feb 22 '16 at 16:58
2

Short and reusable method:

usort($array, 'nameSort');

function nameSort($a, $b)
{
    return strcmp($a['lastName'], $b['lastName']);
}
Justin
  • 26,443
  • 16
  • 111
  • 128
  • 1
    Also note, if you are doing this in a class, you'd pass an array instead of a string to usort, like so: `usort($array, array($this, 'nameSort'));` – Justin Mar 27 '14 at 22:25
2

AS I posted in php.net, you can use this function:

<?php

function sksort(&$array, $subkey="id", $sort_ascending=false) {

    if (count($array))
        $temp_array[key($array)] = array_shift($array);

    foreach($array as $key => $val){
        $offset = 0;
        $found = false;
        foreach($temp_array as $tmp_key => $tmp_val)
        {
            if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey]))
            {
                $temp_array = array_merge(    (array)array_slice($temp_array,0,$offset),
                                            array($key => $val),
                                            array_slice($temp_array,$offset)
                                          );
                $found = true;
            }
            $offset++;
        }
        if(!$found) $temp_array = array_merge($temp_array, array($key => $val));
    }

    if ($sort_ascending) $array = array_reverse($temp_array);

    else $array = $temp_array;
}

?>
SERPRO
  • 10,015
  • 8
  • 46
  • 63