I want to print data one by one using JSON array.
[
{
"meta_device_id":"NEA8000000345",
"name":"Jaffna",
"description":"aq",
"latitude":9.779333,
"longitude":80.04059,
"pm10":31.0,
"pm25":55.0,
"pm100":74.0,
"co2":4996.0,
"temperature":27.94,
"timestamp":1647383654251
},
{
"meta_device_id":"NEA8000011",
"name":"Galle",
"description":"MET Department, Galle",
"latitude":6.0297956,
"longitude":80.21212,
"pm10":28.0,
"pm25":43.0,
"pm100":52.0,
"co2":2264.81,
"temperature":34.59,
"timestamp":1647398582681
}
]
This is my json array. I want to take meta_device_id for variable . How can I do it ?
CodePudding user response:
you need to decode
your result json
and then either loop through your data to get meta_device_id
or you can get value by array index.
$string = '[
{
"meta_device_id":"NEA8000000345",
"name":"Jaffna",
"description":"aq",
"latitude":9.779333,
"longitude":80.04059,
"pm10":31.0,
"pm25":55.0,
"pm100":74.0,
"co2":4996.0,
"temperature":27.94,
"timestamp":1647383654251
},
{
"meta_device_id":"NEA8000011",
"name":"Galle",
"description":"MET Department, Galle",
"latitude":6.0297956,
"longitude":80.21212,
"pm10":28.0,
"pm25":43.0,
"pm100":52.0,
"co2":2264.81,
"temperature":34.59,
"timestamp":1647398582681
}
]';
$yummy = json_decode($string);
Now you can loop through your data and store values in an array.
$myarr = array();
$i=0;
foreach($yummy as $yum)
{
$myarr[$i] = $yum->meta_device_id;
$i ;
}
var_dump($myarr); // array(2) { [0]=> string(13) "NEA8000000345" [1]=> string(10) "NEA8000011" }
OR you can get values by index.
var_dump($yummy[0]->meta_device_id); // NEA8000000345
var_dump($yummy[0]->meta_device_id); // NEA8000011