-2

I'm trying to sort this multidimensional array. I don't want to sort the first dimension of the array by the names that are contained in the second array.

How would I sort this alphabetically by "name":

Array
(
[0] => Array
    (
        ["name"] => "Delta"
        ["other1"] => "other data..."
        ["other2"] => "other data..."
    )

[1] => Array
    (
        ["name"] => "Beta"
        ["other1"] => "other data..."
        ["other2"] => "other data..."
    )

[2] => Array
    (
        ["name"] => "Alpha"
        ["other1"] => "other data..."
        ["other2"] => "other data..."
    )
)

So that it winds up like this:

Array
(
[0] => Array
    (
        ["name"] => "Alpha"
        ["other1"] => "other data..."
        ["other2"] => "other data..."
    )

[1] => Array
    (
        ["name"] => "Beta"
        ["other1"] => "other data..."
        ["other2"] => "other data..."
    )

[2] => Array
    (
        ["name"] => "Delta"
        ["other1"] => "other data..."
        ["other2"] => "other data..."
    )
)

Would much appreciate some help!

clearmend
  • 136
  • 8
  • possible duplicate of [Sort an array by a child array's value in PHP](http://stackoverflow.com/questions/2672900/sort-an-array-by-a-child-arrays-value-in-php) or http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2?rq=1 and many others... have you even tried to search? – peko Oct 10 '13 at 14:33
  • possible duplicate of [Reference: all basic ways to sort arrays and data in PHP](http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php) – deceze Oct 10 '13 at 14:33
  • Have you tried something? – Rafael Barros Oct 10 '13 at 14:37

1 Answers1

0

http://3v4l.org/hVUPI

<?php

$array = array(
    array(
        "name" => "Delta",
        "other1" => "other data...",
        "other2" => "other data...",
    ),
    array(
        "name" => "Beta",
        "other1" => "other data...",
        "other2" => "other data...",
    ),
    array(
        "name" => "Alpha",
        "other1" => "other data...",
        "other2" => "other data...",
    ),
);

usort($array, function($a, $b) {
    return strcmp($a['name'], $b['name']);
});

print_r($array);

Outputs:

Array
(
    [0] => Array
        (
            [name] => Alpha
            [other1] => other data...
            [other2] => other data...
        )

    [1] => Array
        (
            [name] => Beta
            [other1] => other data...
            [other2] => other data...
        )

    [2] => Array
        (
            [name] => Delta
            [other1] => other data...
            [other2] => other data...
        )

)
Hein Andre Grønnestad
  • 6,438
  • 2
  • 28
  • 41