drbfxb977777 2016-12-17 11:18
浏览 36
已采纳

如何解析php中的json数组数据

I want to parse the JSON array $inputjson data with a loop, with one condition: show only those products which quantity != 0

i.e in this case, my output should be like

echo >14516 , 29 //productid, list value
echo  >14517 , 30 //productid, list value
echo >14519 , 32 //productid, list value

json:

$inputjson="{"14514":{"attributes_values":{"3":"7"},"attributes":[27],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":0,"reference":"2221201701017","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'27'"},"14515":{"attributes_values":{"3":"7,5"},"attributes":[28],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":0,"reference":"2221201702014","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'28'"},"14516":{"attributes_values":{"3":"8"},"attributes":[29],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":1,"reference":"2221201703011","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'29'"},"14517":{"attributes_values":{"3":"8,5"},"attributes":[30],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":2,"reference":"2221201704018","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'30'"},"14518":{"attributes_values":{"3":"9"},"attributes":[31],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":0,"reference":"2221201705015","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'31'"},"14519":{"attributes_values":{"3":"9,5"},"attributes":[32],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":1,"reference":"2221201706012","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'32'"},"14520":{"attributes_values":{"3":"10"},"attributes":[33],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":0,"reference":"2221201707019","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'33'"},"14521":{"attributes_values":{"3":"10,5"},"attributes":[34],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":0,"reference":"2221201708016","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'34'"},"14522":{"attributes_values":{"3":"11"},"attributes":[35],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":0,"reference":"2221201709013","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'35'"},"14523":{"attributes_values":{"3":"11,5"},"attributes":[36],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":0,"reference":"2221201710019","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'36'"},"14524":{"attributes_values":{"3":"12"},"attributes":[37],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":0,"reference":"2221201711016","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'37'"},"14525":{"attributes_values":{"3":"13"},"attributes":[39],"price":0,"specific_price":false,"ecotax":0,"weight":0,"quantity":0,"reference":"2221201712013","unit_impact":"0.00","minimal_quantity":"1","available_date":"","id_image":-1,"list":"'39'"}}";

$inputjson=json_decode($inputjson,true);

$inputjson print_r output:--
 Array
    (
        [14514] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 7
                    )

                [attributes] => Array
                    (
                        [0] => 27
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 0
                [reference] => 2221201701017
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '27'
            )

        [14515] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 7,5
                    )

                [attributes] => Array
                    (
                        [0] => 28
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 0
                [reference] => 2221201702014
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '28'
            )

        [14516] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 8
                    )

                [attributes] => Array
                    (
                        [0] => 29
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 1
                [reference] => 2221201703011
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '29'
            )

        [14517] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 8,5
                    )

                [attributes] => Array
                    (
                        [0] => 30
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 2
                [reference] => 2221201704018
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '30'
            )

        [14518] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 9
                    )

                [attributes] => Array
                    (
                        [0] => 31
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 0
                [reference] => 2221201705015
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '31'
            )

        [14519] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 9,5
                    )

                [attributes] => Array
                    (
                        [0] => 32
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 1
                [reference] => 2221201706012
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '32'
            )

        [14520] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 10
                    )

                [attributes] => Array
                    (
                        [0] => 33
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 0
                [reference] => 2221201707019
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '33'
            )

        [14521] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 10,5
                    )

                [attributes] => Array
                    (
                        [0] => 34
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 0
                [reference] => 2221201708016
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '34'
            )

        [14522] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 11
                    )

                [attributes] => Array
                    (
                        [0] => 35
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 0
                [reference] => 2221201709013
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '35'
            )

        [14523] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 11,5
                    )

                [attributes] => Array
                    (
                        [0] => 36
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 0
                [reference] => 2221201710019
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '36'
            )

        [14524] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 12
                    )

                [attributes] => Array
                    (
                        [0] => 37
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 0
                [reference] => 2221201711016
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '37'
            )

        [14525] => Array
            (
                [attributes_values] => Array
                    (
                        [3] => 13
                    )

                [attributes] => Array
                    (
                        [0] => 39
                    )

                [price] => 0
                [specific_price] => 
                [ecotax] => 0
                [weight] => 0
                [quantity] => 0
                [reference] => 2221201712013
                [unit_impact] => 0.00
                [minimal_quantity] => 1
                [available_date] => 
                [id_image] => -1
                [list] => '39'
            )

    )
  • 写回答

2条回答 默认 最新

  • duanjiaolia97750 2016-12-17 11:33
    关注

    Loop through json items and check for quantity. Only those different than 0 will be treated as true in if condition:

    foreach( $inputjson as $item=>$value ) {
      if ( $value['quantity'] ) {
        echo $item . ', ' . $value['list'];
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名