Jump to content

array sort


Vivid Lust

Recommended Posts

Hi all,

 

How could i sort this array so that:

 

index   text            count

1    england          2

2      scotland          5

3      wales            1

 

becomes:

 

index   text            count

2        scotland          5

1      england          2

3        wales            1

 

Thanks all!!

Link to comment
https://forums.phpfreaks.com/topic/192402-array-sort/
Share on other sites

try

<?php
$test = array(1 =>
		array('text' => 'england', 'count' => 2),
		array('text' => 'scotland', 'count' => 5),
		array('text' => 'wales', 'count' => 1)
);
echo '<pre>',print_r($test), '</pre><hr />';

function my_compare($a, $b){
if ($a['count'] == $b['count']) return 0;
return $a['count'] < $b['count'] ? 1 : -1;
}

uasort($test, 'my_compare');

echo '<pre>',print_r($test), '</pre><hr />';
?>

Link to comment
https://forums.phpfreaks.com/topic/192402-array-sort/#findComment-1013914
Share on other sites

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.