Json 结果排序在 php 中不起作用


Json results sorting not working in php

排序不起作用。

我的控制器页面

$price = $_POST['price'];
        $search = $_POST['search'];
        $cat_id = $_POST['category'];
        $post1 = file_get_contents("...?search=".$search."&category=".$cat_id);
        $products = CJSON::decode($post1, true);
        if($price == 1)
        {
            function prod($a, $b) {
                        return $a["retail_price"] - $b["retail_price"];
                }
                 usort($products, "prod");                                                                                                                                                                                                   
            echo "<pre>";print_r($products);die;
        }

它不会按排序顺序打印结果。$products是一个数组

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 11027
                    [title] => SPIDERMAN ENGRAVED LIGHTER
                    [barcode] => LEN-0022
                    [qty] => 8
                    [url] => http://www.ebay.com/itm/-/381279328138?
                    [retail_price] => 9.99
                    [category] => Array
                        (
                            [id] => 34
                            [name] => ENGRAVED LIGHTERS
                        )
                    [bin] => Array
                        (
                            [id] => 346
                            [name] => F10
                        )
                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [small] => http://www.heygidday.biz/portal//timthumb.php?src=/files/products/dscn124900.jpg&w=30
                                    [middle] => http://www.heygidday.biz/portal//timthumb.php?src=/files/products/dscn124900.jpg&w=100
                                    [source] => http://www.heygidday.biz/portal/files/products/dscn124900.jpg
                                )
                        )
                )
            [1] => Array
                (
                    [id] => 11548
                    [title] => SPIDER MAN Black Lighter
                    [barcode] => LEN-0067
                    [qty] => 6
                    [url] => http://www.ebay.com/itm/-/361369988738?
                    [retail_price] => 10.99
                    [category] => Array
                        (
                            [id] => 34
                            [name] => ENGRAVED LIGHTERS
                        )
                    [bin] => Array
                        (
                            [id] => 346
                            [name] => F10
                        )
                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [small] => http://www.heygidday.biz/portal//timthumb.php?src=/files/products/len-00670.jpg&w=30
                                    [middle] => http://www.heygidday.biz/portal//timthumb.php?src=/files/products/len-00670.jpg&w=100
                                    [source] => http://www.heygidday.biz/portal/files/products/len-00670.jpg
                                )
                            )
                    )
            )
    )

这是我在数组按retail_price排序时得到的结果。排序不起作用,为什么会这样????谁能帮我解决这个问题????等待回应...

$products不仅仅是一个数组,而是数组中的数组:

$products = array( array( product1, product2 ) );

你不想要周围的数组,所以就做

    $post1 = file_get_contents("...?search=".$search."&category=".$cat_id);
    $data = CJSON::decode($post1, true);
    $products = $data[0];
    //...

站点说明:不应在运行的代码中声明函数。这往往会导致奇怪的错误。

相反,只需在顶部声明它,或使用匿名函数。

$prod = function($a, $b) {
    return $a["retail_price"] - $b["retail_price"];
}
usort($products, $prod);