在PHP的价格范围内列出json数组

时间:2016-07-16 09:25:24

标签: php arrays json sorting

我遇到了问题。我需要在一个范围内列出json数组元素。不知道怎么办........

控制器页面

$search = $_POST['search'];
$cat_id = $_POST['category'];
$post1 = file_get_contents("....?search=".$search."&category=".$cat_id);
$data = CJSON::decode($post1, true);
$products = $data['products'];
$pricerange = $_POST['pricerange'];
$myArray = explode('-',$pricerange);
$a =explode('$',$myArray[0]);
$b =explode('$',$myArray[1]);

$a是下限,$b是上限,$products是必须列出的json数组。

我需要知道如何根据$products和`$ b'之间的价格范围列出$a。 产品的产出

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
                        )

                )

        )

)

请帮我这个.......

1 个答案:

答案 0 :(得分:0)

我为你创建了一个小脚本,应该有帮助

    function sortbyprice($a, $b) {
        return $a['retail_price'] - $b['retail_price'];
    }
    $result_Array = array(
        array('id'=>'11027','retail_price'=>'9.99'),
        array('id'=>'11548','retail_price'=>'10.99'),
        array('id'=>'11528','retail_price'=>'3.99'),
        array('id'=>'18548','retail_price'=>'55.99')
    );
echo '<pre>';
print_r($result_Array);
usort($result_Array, 'sortbyprice');
print_r($result_Array);