-1

I have a multidimensional array that needs to be reoreder, the array look like this :

[products] => Array
                    (
                    [149] => Array
                        (
                            [name] => Ichikami1
                            [qty] => 2
                        )

                    [150] => Array
                        (
                            [name] => Ichikami2
                            [qty] => 4
                        )

                    [377] => Array
                        (
                            [name] => BCL
                            [qty] => 2
                        )

                )

inside the child array there is 'qty' index, i want to sort the child array by 'qty' index in descending order, so it will look like this:

[products] => Array
                    (

                        [0] => Array
                            (
                                [name] => Ichikami2 
                                [qty] => 4
                            )

                        [1] => Array
                            (
                                [name] => Ichikami1 
                                [qty] => 2
                            )

                        [2] => Array
                            (
                                [name] => BCL 
                                [qty] => 2
                            )

                    )

is there a way to do this?

Hunter
  • 459
  • 4
  • 15
  • http://php.net/manual/en/function.usort.php – Peter Chaula Nov 28 '17 at 06:29
  • Have you went through, [this](https://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php), [this](https://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value) and [this](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php)? – Rahul Nov 28 '17 at 06:31
  • post sample code you tried – User123456 Nov 28 '17 at 06:33

1 Answers1

0

Simply by using array_multisort(), You can do this like:

$qty = array();
foreach ($products as $key => $row)
{
    $qty[$key] = $row['qty'];
}
array_multisort($qty, SORT_DESC, $products);
helpdoc
  • 1,910
  • 14
  • 36