• 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.

    $data = array($nickname => $num_rows);
    arsort($data);
    foreach ($data as $key => $value) {
    echo $key . ‘ : ‘ . $value. ‘
    ‘;
    }

Viewing 1 replies (of 1 total)
  • I’m a usort fan:

    function compareNumber($data_1, $data_2) {
      if ($data_1->num_rows == $data_2->num_rows) return 0;
      return ($data_1->num_rows < $data_2->num_rows) ? -1 : 1;
    }
    usort($data, 'compareNumber');

Viewing 1 replies (of 1 total)

The topic ‘Sorting Array with User Id’ is closed to new replies.