Jump to content

Sorting Multidimensional Array


peterhuynh

Recommended Posts

Hi, supernoob here. This is my 2nd thread. The 1st one was very helpful.

 

I want to sort a multidimensional array.

 

I have something like this:

 

{"bids": [[ 100, 24 ], [ 300, 10 ], [ 200, 34 ]], "asks": [[ 300, 23 ], [ 100, 34 ], [ 200, 21]]}

 

I want to order "bids" such that the largest first number in the square brackets goes first. (I realize I'm not explaining this well.) For example, I want "[[ 300, 10 ], [ 200, 34 ], [ 100, 24 ]]".

 

And I want to order "asks" such that the smallest first number in the square bracket goes first. For example, I want "[[ 100, 34 ], [ 200, 21 ], [ 300, 23]]"

 

 

 

Thank you so much!

 

 

Link to comment
https://forums.phpfreaks.com/topic/294604-sorting-multidimensional-array/
Share on other sites

try

$arr = json_decode('{"bids": [[ 100, 24 ], [ 300, 10 ], [ 200, 34 ]], "asks": [[ 300, 23 ], [ 100, 34 ], [ 200, 21]]}', 1);

rsort($arr['bids']);
sort($arr['asks']);
echo '<pre>',print_r($arr, true),'</pre>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.