Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3228988
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:45:28+00:00 2026-05-17T16:45:28+00:00

I have an array of rows from a database I need to sort by

  • 0

I have an array of rows from a database I need to sort by two parameters. I have tried to use the usort() function to accomplish this, but I’m running into some trouble.

Here is my code:

if ($sort == 'rating-desc') usort($records, array('browse_model', 'cmp'));
private function cmp($a, $b) {
    $ratingCmp = strcmp($b['rating'], $a['rating']);
    if ($ratingCmp == 0) {
        return strcmp($b['title'], $a['title']);
    } else {
        return $ratingCmp;
    }
}

Here is the print_r() result of the array before usort():

Array
(
[0] => Array
    (
        [isbn] => 1847199488
        [title] => CodeIgniter 1.7
        [rating] => 3.5
    )

[1] => Array
    (
        [isbn] => 059615593X
        [title] => CSS Cookbook, 3rd Edition (Animal Guide)
        [rating] => 3.5
    )

[2] => Array
    (
        [isbn] => 0321637984
        [title] => Essential Facebook Development: Build Successful Applications for the Facebook Platform
        [rating] => 3.5
    )

[3] => Array
    (
        [isbn] => 0980576857
        [title] => jQuery: Novice to Ninja
        [rating] => 4.5
    )

[4] => Array
    (
        [isbn] => 0596157134
        [title] => Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)
        [rating] => 4.5
    )

)

And here is the the result after the usort() (sorted by rating, but not by title):

Array
(
[0] => Array
    (
        [isbn] => 0980576857
        [title] => jQuery: Novice to Ninja
        [rating] => 4.5
    )

[1] => Array
    (
        [isbn] => 0596157134
        [title] => Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)
        [rating] => 4.5
    )

[2] => Array
    (
        [isbn] => 0321637984
        [title] => Essential Facebook Development: Build Successful Applications for the Facebook Platform
        [rating] => 3.5
    )

[3] => Array
    (
        [isbn] => 1847199488
        [title] => CodeIgniter 1.7
        [rating] => 3.5
    )

[4] => Array
    (
        [isbn] => 059615593X
        [title] => CSS Cookbook, 3rd Edition (Animal Guide)
        [rating] => 3.5
    )

)

So it puts them in order by rating, but not by title. How can I modify this so it sorts by rating, then by title?

For what it’s worth, I’ve also tried this:

if ($sort == 'rating-desc') {
        foreach ($records as $key => $row) {
            $rating[$key] = $row['rating'];
            $title[$key] = $row['title'];
        }
        array_multisort($title, SORT_DESC, $rating, SORT_ASC, $records);
    }

But that result isn’t correct either. It shows the same result as the usort I tried above.

What am I missing?

Thanks much,
Marcus

EDIT: Following some of the suggestions below, here are several things I’ve tried. None of them solve my problem. They all return the ratings sorted properly, but the title is descending.

private function cmp($a, $b) {
    if ($a['rating'] == $b['rating']) {
        return strcasecmp($b['title'], $a['title']);
    }
    return $b['rating'] - $a['rating'];
}

private function cmp($a, $b) {
    if ($a['rating'] == $b['rating']) 
        return strcasecmp($a['title'], $b['title']);
    return $b['rating'] - $a['rating'];
}

    if ($sort == 'rating-desc') {
        foreach ($records as $key => $row) {
            $rating[$key] = $row['rating'];
            $title[$key] = $row['title'];
        }
        array_multisort($rating, SORT_DESC, $title, SORT_ASC, $records);
    }

    if ($sort == 'rating-desc') {
        foreach ($records as $key => $row) {
            $rating[$key] = $row['rating'];
            $title[$key] = $row['title'];
        }
        array_multisort($rating, SORT_DESC, $title, SORT_DESC, $records);
    }

Here is my entire code example – the entire model…

class Browse_model extends Model {

    function Browse_model() {
        parent::Model();
    }

    function get_form_tags() {
        $sql = 'select t.id, t.tag, coalesce(btc.count, 0) as count from tags as t left outer join (select tag_id, count(*) as count from books_tags group by tag_id) as btc on t.id = btc.tag_id order by count desc, tag asc';
        $query = $this->db->query($sql);
        $tags = $query->result();
        return $tags;
    }

    function get_book_info($tags, $andor, $read, $sort) {

        /*
         * SELECT b.isbn, b.title, b.publisher, b.date, b.thumb, b.filename, b.pages, t.tag
         * FROM books AS b
         * INNER JOIN books_tags AS bt ON b.isbn = bt.book_id
         * INNER JOIN tags AS t ON bt.tag_id = t.id
         * ORDER BY b.title, t.tag
         */

        switch ($sort) {
            case 'alpha-desc':
                $order = 'b.title DESC, t.tag';
                break;
            case 'date-desc':
                $order = 'b.date DESC, b.title, t.tag';
                break;
            case 'date-asc':
                $order = 'b.date, b.title, t.tag';
                break;
            default:
                $order = 'b.title, t.tag';
                break;
        }

        $this->db->select('b.isbn, b.title, b.publisher, b.date, b.thumb, b.filename, b.pages, t.tag');
        $this->db->from('books AS b');
        $this->db->join('books_tags AS bt', 'b.isbn = bt.book_id', 'inner');
        $this->db->join('tags AS t', 'bt.tag_id = t.id', 'inner');
        $this->db->order_by($order);
        $query = $this->db->get();
        $result = $query->result();

        $counter = '';
        $records = $meta = $tags = array();
        $count = count($result);
        $i = 1;

        foreach ($result as $book) {
            // If this is not the last row
            if ($i < $count) {
                // If this is the first appearance of this book
                if ($counter != $book->isbn) {
                    // If the meta array already exists
                    if ($meta) {
                        // Add the combined tag string to the meta array
                        $meta['tags'] = implode(', ', $tags);
                        // Add the meta array
                        $records[] = $meta;
                        // Empty the tags array
                        $tags = array();
                    }
                    // Reset the counter
                    $counter = $book->isbn;
                    // Grab the book from Amazon
                    $amazon = $this->amazon->get_amazon_item($book->isbn);
                    // Collect the book information
                    $meta = array(
                        'isbn' => $book->isbn,
                        'title' => strip_slashes($book->title),
                        'publisher' => strip_slashes($book->publisher),
                        'date' => date('F j, Y', strtotime($book->date)),
                        'thumb' => $book->thumb,
                        'file' => $book->filename,
                        'pages' => $book->pages,
                        'rating' => $amazon->Items->Item->CustomerReviews->AverageRating,
                        'raters' => $amazon->Items->Item->CustomerReviews->TotalReviews
                    );
                    // Add the tag to the tags array
                    $tags[] = $book->tag;
                } else {
                    // All we need is the tag
                    $tags[] = $book->tag;
                }
            // If this is the last row
            } else {
                // If this is the first appearance of this book
                if ($counter != $book->isbn) {
                    // Grab the book from Amazon
                    $amazon = $this->amazon->get_amazon_item($book->isbn);
                    // Collect the book information
                    $meta = array(
                        'isbn' => $book->isbn,
                        'title' => strip_slashes($book->title),
                        'publisher' => strip_slashes($book->publisher),
                        'date' => date('F j, Y', strtotime($book->date)),
                        'thumb' => $book->thumb,
                        'file' => $book->filename,
                        'pages' => $book->pages,
                        'rating' => $amazon->Items->Item->CustomerReviews->AverageRating,
                        'raters' => $amazon->Items->Item->CustomerReviews->TotalReviews
                    );
                }
                // All we need is the tag
                $tags[] = $book->tag;
                // Add the combined tag string to the meta array
                $meta['tags'] = implode(', ', $tags);
                // Add the meta array
                $records[] = $meta;
            }
            $i++;
        }

        echo '<code><pre>';
        print_r($records);
        echo '</pre></code>';

        if ($sort == 'rating-desc') usort($records, array('browse_model', 'cmp'));

        echo '<code><pre>';
        print_r($records);
        echo '</pre></code>';
        return $records;
    }

    private function cmp($a, $b) {
        if ($a['rating'] == $b['rating']) 
            return strcasecmp($b['title'], $a['title']);
        return $b['rating'] - $a['rating'];
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:45:29+00:00Added an answer on May 17, 2026 at 4:45 pm

    You could alternatively use array_multisort()

    deleted code

    Creates a couple extra arrays, but gets the job done; if you put the code into a function or a method as you’re doing (pass $records by reference) those will get destroyed on function exit.

    Edit: I see you’ve put into your question that you’ve tried this. How doesn’t it work?

    Edit: This works for me:

    foreach ($records as $rec) {
        $rating[] = $rec['rating'];
        $title[] = strtolower($rec['title']);
    }
    
    array_multisort($title, SORT_DESC, $rating, SORT_ASC, $records);
    
    print_r($records);
    

    Another edit:

    Given this array:

    $records = array(
      0 => 
      array (
        'isbn' => '1847199488',
        'title' => 'CodeIgniter 1.7',
        'rating' => 3.5,
      ),
      1 => 
      array (
        'isbn' => '1847199488',
        'title' => 'CodeIgniter 1.7',
        'rating' => 3,
      ),
      2 => 
      array (
        'isbn' => '1847199488',
        'title' => 'CodeIgniter 1.7',
        'rating' => 3.2,
      ),
      3 => 
      array (
        'isbn' => '1847199488',
        'title' => 'CodeIgniter 1.7',
        'rating' => 4.5,
      ),
      4 => 
      array (
        'isbn' => '059615593X',
        'title' => 'CSS Cookbook, 3rd Edition (Animal Guide)',
        'rating' => 3.5,
      ),
      5 => 
      array (
        'isbn' => '0321637984',
        'title' => 'Essential Facebook Development: Build Successful Applications for the Facebook Platform',
        'rating' => 3.5,
      ),
      6 => 
      array (
        'isbn' => '0980576857',
        'title' => 'jQuery: Novice to Ninja',
        'rating' => 4.5,
      ),
      7 => 
      array (
        'isbn' => '0596157134',
        'title' => 'Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)',
        'rating' => 4.5,
      ),
    );
    

    This code:

    foreach ($records as $rec) {
        $rating[] = $rec['rating'];
        $title[] = strtolower($rec['title']);
    }
    
    array_multisort($rating, SORT_DESC, SORT_NUMERIC, $title, SORT_ASC, SORT_STRING, $records);
    
    var_export($records);
    

    Will produce this:

    array (
      0 => 
      array (
        'isbn' => '1847199488',
        'title' => 'CodeIgniter 1.7',
        'rating' => 4.5,
      ),
      1 => 
      array (
        'isbn' => '0980576857',
        'title' => 'jQuery: Novice to Ninja',
        'rating' => 4.5,
      ),
      2 => 
      array (
        'isbn' => '0596157134',
        'title' => 'Learning PHP, MySQL, and JavaScript: A Step-By-Step Guide to Creating Dynamic Websites (Animal Guide)',
        'rating' => 4.5,
      ),
      3 => 
      array (
        'isbn' => '1847199488',
        'title' => 'CodeIgniter 1.7',
        'rating' => 3.5,
      ),
      4 => 
      array (
        'isbn' => '059615593X',
        'title' => 'CSS Cookbook, 3rd Edition (Animal Guide)',
        'rating' => 3.5,
      ),
      5 => 
      array (
        'isbn' => '0321637984',
        'title' => 'Essential Facebook Development: Build Successful Applications for the Facebook Platform',
        'rating' => 3.5,
      ),
      6 => 
      array (
        'isbn' => '1847199488',
        'title' => 'CodeIgniter 1.7',
        'rating' => 3.2,
      ),
      7 => 
      array (
        'isbn' => '1847199488',
        'title' => 'CodeIgniter 1.7',
        'rating' => 3,
      ),
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a largish table of data pulled from my database (~1500 rows, each
I have an array being returned from the database that looks like so: $data
I have a data result being returned from my database with multiple rows. The
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have an array in Perl: my @my_array = (one,two,three,two,three); How do I remove
I have an array of shorts (short[]) that I need to write out to
I am going nuts here, I have an array of checkboxes from a form
i have the following code for search from database show return result to the
I have this PHP class, whose purpose is to fetch some configuration data from
Consider this: One mySQL database that has tables and rows and data within it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.