Decode Json Object Php

134 Views Asked by At

My json data looks like this :

{
 "amount":550,
 "items":[
    {"item_id":12334, "price": 504}, 
    {"item_id":12335, "price":206}
  ]
}

I want to know how to parse this data in order to fetch each information in separate variable.

3

There are 3 best solutions below

2
On BEST ANSWER

In PHP you can use json_decode($myJsonString), while in Android you might look for a third party library, GSON is a good one though

$data = json_decode($json);
foreach($data->items as $item) {
   echo $item->item_id . ' ' . $item->price;
}
1
On
0
On

Create local variable suppose $response;

store your output in $response;

You can fetch price by following code.

foreach ($response->items as $item){

 echo $item->price;
 echo "<br>";

}