Razvan Zamfir Razvan Zamfir - 1 year ago 81
PHP Question

Sort multidimensional array alphabetically, by value and store it in a new variable

I have the following array I want to sort by value, A to Z, and store in a new arary

$atoz_people
:

$people = (
[0] => Array
(
[id] => 1
[name] => Ken
)

[1] => Array
(
[id] => 2
[name] => Anne
)

[2] => Array
(
[id] => 3
[name] => Bob
)

);


I used the "classic"
asort($people)
:

$atoz_people = $people;
asort($atoz_people);
print_r($atoz_people);


It does not work, unfortunately. What shall I change in order to get the array below? Is it even possible?

$atoz_people = (
[1] => Array
(
[id] => 2
[name] => Anne
)
[2] => Array
(
[id] => 3
[name] => Bob
)
[0] => Array
(
[id] => 1
[name] => Ken
)

);

Answer Source

For multi dimensional you can use methods like usort or uasort

uasort($people,"sort_name");
var_dump($people);
function sort_name($a,$b)
{
  return $a["name"] > $b["name"];
}

check link

OR

link

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download