-1
$response='{"Data":[{"item_name":"FL001(1)","Qty":"50","color":"red","Rate":"50","Amt":"2500"},
{"item_name":"FL001(1)","Qty":"50","color":"red","Rate":"50","Amt":"2500"},
{"item_name":"FL001(1)","Qty":"50","color":"red","Rate":"50","Amt":"2500"}

This is my JSON response. How do I loop through the values?

James Jones
  • 3,850
  • 5
  • 25
  • 44

2 Answers2

1

This might help.

$array = json_decode($string, true);

foreach ($array as $key => $val) {
    echo $val; 
}
Vamsi
  • 423
  • 1
  • 5
  • 19
1

There is an error on your JSON string

 <?php

 $response='{"Data":
 [{"item_name":"FL001(1)","Qty":"50","color":"red","Rate":"50","Amt":"2500"},
 {"item_name":"FL001(1)","Qty":"50","color":"red","Rate":"50","Amt":"2500"},
 {"item_name":"FL001(1)","Qty":"50","color":"red","Rate":"50","Amt":"2500"}]}';
                                //this two closing brackets you are missing^^

$parsed = json_decode($response, true);

$what_you_need = $parsed["Data"];

//example output with a loop
foreach($what_you_need as $key=> $value){
  echo "key: ".$key  //key
     . ", item_name: ".$value["item_name"]."</br>"  //item name + a linebreak
     ."Qty: ".$value["Qyt"]  //Qyt value
     .", color: ".$value["color"]  //color
     .", Rate: ".$value["Rate"]    //rate
     .", Amt: ".$value["Amt"]."</br></hr>";  //Amt + linebreak and header row
}
Francisco Hahn
  • 435
  • 5
  • 10