doujiao8491 2015-08-27 11:30
浏览 424
已采纳

循环遍历php中的json对象

I've json object and I want to make addition of 'quantity' field in it. My code is here

while($row=mysql_fetch_array($result)){
   $posts = json_decode($row['value'],true);
   array_push($response,$posts);
  }
   //print_r($response);
   foreach($response as $value){
         //$plus +=1;
         $plus = $plus+$value['quantity'];
   }

print_r($response) then gives:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [srno] => 1
                    [name] => Gaspari Aminolast
                    [quantity] => 2
                    [price] => 2920.0000
                    [total_bill] => 5840
                )

            [1] => Array
                (
                    [srno] => 2
                    [name] => Gaspari Amino Max
                    [quantity] => 2
                    [price] => 2640.0000
                    [total_bill] => 5280
                )

            [2] => Array
                (
                    [srno] => 3
                    [name] => Myofusion 10Lbs
                    [quantity] => 2
                    [price] => 8400.0000
                    [total_bill] => 16800
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [srno] => 1
                    [name] => Gaspari Aminolast
                    [quantity] => 2
                    [price] => 2920.0000
                    [total_bill] => 5840
                )

            [1] => Array
                (
                    [srno] => 2
                    [name] => Gaspari Amino Max
                    [quantity] => 2
                    [price] => 2640.0000
                    [total_bill] => 5280
                )

            [2] => Array
                (
                    [srno] => 3
                    [name] => Myofusion 10Lbs
                    [quantity] => 2
                    [price] => 8400.0000
                    [total_bill] => 16800
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [srno] => 1
                    [name] => Gaspari Aminolast
                    [quantity] => 2
                    [price] => 2920.0000
                    [total_bill] => 5840
                )

            [1] => Array
                (
                    [srno] => 2
                    [name] => Gaspari Amino Max
                    [quantity] => 2
                    [price] => 2640.0000
                    [total_bill] => 5280
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [srno] => 1
                    [name] => Gaspari Aminolast
                    [quantity] => 2
                    [price] => 2920.0000
                    [total_bill] => 5840
                )

            [1] => Array
                (
                    [srno] => 2
                    [name] => Gaspari Amino Max
                    [quantity] => 2
                    [price] => 2640.0000
                    [total_bill] => 5280
                )

        )

    [4] => Array
        (
        )

    [5] => Array
        (
        )

    [6] => Array
        (
            [0] => Array
                (
                    [srno] => 1
                    [name] => Gaspari Amino Max
                    [quantity] => 2
                    [price] => 2640.0000
                    [total_bill] => 5280
                )

            [1] => Array
                (
                    [srno] => 2
                    [name] => Gaspari Aminolast
                    [quantity] => 2
                    [price] => 2920.0000
                    [total_bill] => 5840
                )

        )

)
  • 写回答

2条回答 默认 最新

  • dongliu0823 2015-08-27 11:40
    关注

    try this

    plus = 0;
    foreach($response as $value){
         //$plus +=1;
       foreach($value as $val)
       {
         $plus = $plus+$val['quantity'];
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配