0

I can find an answer to this if the first dimension of the array is an offset, but not if they are keys. I need to sort the array by the value of an inner key, while keeping the main array's keys intact.

Consider this example multi-array..

$test['first']['count']=23;
$test['first']['name']='foo';
$test['first']['type']='red';

$test['second']['count']=54;
$test['second']['name']='bar';
$test['second']['type']='green';

$test['third']['count']=11;
$test['third']['name']='foobar';
$test['third']['type']='blue';

So I would like to order the array highest to lowest based on 'count'. Looking at the above, the order would be this...

$test['second']
$test['first']
$test['third']

How can it be solved? Help appreciated, thanks!

Chibi
  • 95
  • 1
  • 1
  • 7
  • 2
    `uasort($test, function($a, $b) {return $b['count'] - $a['count']; });` – Mark Baker Mar 13 '16 at 23:38
  • `array_multisort( $test, SORT_DESC );` BTW, “order the array highest to lowest based on 'count'” => second/first/third, not second/third/first – fusion3k Mar 13 '16 at 23:42
  • @fusion3k - [Looks right to me](https://3v4l.org/86CZn), result by count value is 54, 23, 11 - which I take to be descending value – Mark Baker Mar 13 '16 at 23:44
  • @MarkBaker now yes :) (before was $a- $b ) – fusion3k Mar 13 '16 at 23:47
  • @MarkBaker thank you kindly, that solves it perfectly. I don't really get how, but I used it on my array (which is a little more complicated) but it performs exactly how I needed. THANK YOU. – Chibi Mar 13 '16 at 23:58
  • @fusion3k - I tried array_multisort() on my own but 'count' wasn't necessarily the lowest-alphabetically-named key, and some of the other keys may have had values as well bigger than count. So it wasn't working. Appreciated anyway. I also updated my example. You were right, my mistake! – Chibi Mar 14 '16 at 00:00
  • @Chibi it is doesn't need to be 'lowest-alphabetically': do you intend that 'count' is not the first? in this case, yes, it doesn't work. – fusion3k Mar 14 '16 at 00:17

0 Answers0