0

var_export($array) of an array gives me this:

array 
    ( 

     0 => array ( 'id_20200514222532' => '4', ), 
     1 => array ( 'id_20200521123813' => '5', ), 
     2 => array ( 'id_20200521125410' => '8', ), 
     3 => array ( 'id_20200523003107' => '3', ), 
     4 => array ( 'id_20200523214047' => '2', ), 

    )

It should be sorted in descending order based on the numbers, so first 8, second 5 and so on...

john
  • 1,127
  • 3
  • 10
  • 1
    That's some odd data.. all the nested arrays only have *one* element? – user2864740 May 25 '20 at 20:49
  • 1
    Does this answer your question? [How can I sort arrays and data in PHP?](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – Balastrong May 25 '20 at 20:51

1 Answers1

3

You can use usort() for this, a sorter function that accepts a callback for comparing two values

usort($array, function ($a, $b) {
    return reset($b) - reset($a);
});

That callback function we gave usort() will get two "random" items of the array. I used reset($a) and reset($b) to get the first values out of the child arrays, then I compared them with a simple subtraction.

Hunman
  • 155
  • 6