如何正确编写这个usort函数

Mar*_*cus 1 php

我有一个数组的行数我需要按两个参数排序.我试图使用该usort()功能来实现这一目标,但我遇到了一些麻烦.

这是我的代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是print_r()以前数组的结果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
    )

)
Run Code Online (Sandbox Code Playgroud)

以下是结果usort()(按评分排序,但不按标题排序):

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
    )

)
Run Code Online (Sandbox Code Playgroud)

所以它通过评级将它们排序,而不是按标题排列.我如何修改它,以便按评级排序,然后按标题排序?

为了它的价值,我也试过这个:

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);
    }
Run Code Online (Sandbox Code Playgroud)

但结果也不正确.它显示了与我上面尝试的usort相同的结果.

我错过了什么?

非常感谢,
马库斯

编辑:按照下面的一些建议,这里有几件我尝试过的事情.他们都没有解决我的问题.他们都返回正确排序的评级,但标题正在下降.

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);
    }
Run Code Online (Sandbox Code Playgroud)

这是我的整个代码示例 - 整个模型......

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'];
    }


}
Run Code Online (Sandbox Code Playgroud)

cHa*_*Hao 5

您的数组按评级和标题排序.问题是你在区分大小写,而'a'在'Z'之后.

尝试使用strcasecmp而不是strcmp比较标题.

此外,如果评级始终是一个数字,您可能应该在数字上而不是按字母顺序比较评级.用$b['rating'] - $a['rating']而不是strcmp.

编辑:

我采用了你原来的相同功能和相同的数据,并制作了一个测试脚本.随着类中的功能和标记private,PHP警告回调usort是无效的,但继续运行(不接触数组).public但是,通过标记功能,它可以按预期工作.这让我相信PHP讨厌您的回调是私有函数,或者类名错误.

如果你想能够使用私有函数,那么调用usort显然需要在同一个类中的函数中.否则usort抱怨回调无效并拒绝运行.

class Stuff
{
    public function sort(&$arr)
    {
        usort($arr, array('Stuff', 'cmp'));
    }

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

Stuff::sort($records);
print_r($records);
Run Code Online (Sandbox Code Playgroud)

对我来说,这会打印出按评级递减的书籍,然后按标题递增.如果这对你不起作用,那就是时髦的东西.