-1

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
    )    

);
Razvan Zamfir
  • 4,237
  • 4
  • 24
  • 201
  • Possible duplicate of [Sort Multi-dimensional Array by Value](https://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value) –  Jul 27 '17 at 09:21
  • First of all you have to define the problem. How do you know that *"it does not work"*? What is the expected output? And **why** is *that* the expected output and not the one you have now? – axiac Jul 27 '17 at 09:21
  • It doesnt work cause its a multidimensional array. asort is for a normal array. You need usort. – Loko Jul 27 '17 at 09:22
  • The solution to your exact problem is described in the documentation of [`array_multisort()`](http://php.net/manual/en/function.array-multisort.php). – axiac Jul 27 '17 at 09:25
  • @axiac I am making a _select box_ out of it, I need the _options_ to be sorted. – Razvan Zamfir Jul 27 '17 at 09:28
  • Of course they need to be sorted. But you failed to express the only thing that matters: the sorting criteria. They are sorted right now. They are sorted after you call `asort()`. They are probably also sorted in posted expected output. The criteria differ in each case and without clear sorting criteria your question is ambiguous. – axiac Jul 27 '17 at 09:30
  • 1
    I would go one step back to the code that creates this array. I guess it fetches the data from the database. Work on that code and make it return a one-level array that associates the IDs to names. Then the sorting becomes trivial (and `asort()` works as expected). – axiac Jul 27 '17 at 09:32
  • @RazvanZamfir Have you checked my answer? – B. Desai Jul 27 '17 at 10:00
  • @B.Desai Yes, it does not work. – Razvan Zamfir Jul 27 '17 at 10:01
  • I have added link for demo also. It exactly gives what you want try it. @RazvanZamfir – B. Desai Jul 27 '17 at 10:04

2 Answers2

3

1) Store the inital array in a new variable:

$atoz_people = $people;

2) Create sorting function:

function sort_by_name($a,$b)
{
    return $a["name"] > $b["name"];
}

3) Sort array

uasort($atoz_people,"sort_by_name");
print_r_html($atoz_people);

@B. Desai: many thanks!

Razvan Zamfir
  • 4,237
  • 4
  • 24
  • 201
1

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

B. Desai
  • 16,264
  • 5
  • 24
  • 44