Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've following json object and I want to make addition of 'quantity' field in it. My code is:
PHP
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)=
VB
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
                )

        )

)



Here I always get $pluse=0. What I should do.
Posted
Comments
Mohibur Rashid 28-Aug-15 4:59am    
First read your array carefully. The array you have posted. If you read it carefully, you would see
response[0][0][srno]=1
response[0][0][name]=Gaspari Aminolast
response[0][0][quantity]=2
response[0][0][price]=2920.0000
response[0][0][total_bill]=5840

response[0][1][srno]=2
response[0][1][name]=Gaspari Amino Max
response[0][1][quantity]=2
response[0][1][price]=2640.0000
response[0][1][total_bill]=5280
...
...

response[6][1][srno]=2
response[6][1][name]=Gaspari Aminolast
response[6][1][quantity]=2
response[6][1][price]=2920
response[6][1][total_bill]=5840

you are only going through your top array;
i.e.
foreach($response as $value)
$value is two dimensional

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900