dongshan6870 2016-07-15 10:59
浏览 49
已采纳

Json结果排序不能在php中工作

Sorting not working..

my controller page

$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;
        }

it does not print the result in a sorted order.. $products is an array

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
                                )
                            )
                    )
            )
    )

This is the result i get when array is sorted by the retail_price.Sorting does not work ,why is it so???? Can anyone help me with this???? Waiting for the response...........

  • 写回答

1条回答 默认 最新

  • ds000001 2016-07-15 11:14
    关注

    $products is not just an array, but it's an array in an array:

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

    You don't want that surrounding array, so just do

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

    Site note: You shouldn't declare a function within the running code. This tends to result in strange errors.

    Instead just declare it in the top, or use an anonymous function.

    $prod = function($a, $b) {
        return $a["retail_price"] - $b["retail_price"];
    }
    
    usort($products, $prod);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算