Sorting Array with User Id

I’ve got a function that that retrieves all the user names along with the number of published posts. Then, I put both of those variables into an array. This works great, but I would like to sort the array by the number of posts. So in this case sort it by the Value.

I know there is a asort(), but it does not to seem have effect on the order of the array.

Not sure what I'm doing wrong.

Thanks, the code is below.

[php]
$data = array($nickname => $num_rows);
arsort($data);
foreach ($data as $key => $value) {
echo $key . ’ : ’ . $value. '; }
[/php]

What does your array look like?

[php]echo ‘

’ . print_r($data, 1) . ‘
’;[/php]

The output from that was this …

Array
(
[Administrator] => 6
)

Array
(
[Mari Alonso] => 1
)

Array
(
[mikey4011] => 1
)

Array
(
[Nickie Summers] => 10
)

Array
(
[Casidy Summers] => 2
)

Array
(
[LisaM] => 1
)

Array
(
[diane] => 3
)

Array
(
[Madeline Grace] => 1
)

Array
(
[JJK] => 1
)

Array
(
[Aura] => 1
)

It would appears that you’re getting your results from a database. Why would you not sort the results in your query?

You should always have the database do the work that it can.

I would love to, but I’m actually get the number portion of the data from the “row number count” (mysql_num_rows) so I cannot get that directly from the database.

Pretty sure your mistaken about that. Why not let the experts take a look at your DB. From what you are saying, it would be impossible to get the data results the way you want without php and I know that is just not so. Post a sql dump of your db.

I figured it out. The problem was I was making a new array after every “while” instead of appending the data to one array.

I’m using $array[‘key1’] = ‘one’; now and that builds the just one array, which arsort() sorts perfectly.

Thanks for the help.

Thats great you learned something. I still think you could get the same result straight out of the database.

Sponsor our Newsletter | Privacy Policy | Terms of Service