0

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 ?

Buddika
  • 13
  • 1
  • 2
    Does this answer your question? [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) – Andrea Olivato Mar 18 '22 at 05:30
  • @AndreaOlivato Thanks for the replay. I check this example also but always show empty. '$json = file_get_contents('http://123.231.44.147/nbro-invoker/api/sensor/flat-info'); $yummy = json_decode($json); echo $yummy->meta_device_id; This is the code I use. – Buddika Mar 18 '22 at 05:42
  • 1
    That's because the API returns an array of json objects, so trying to access the meta_device_id property will not work. You'd have to specify which device you want to access either by using the index, e.g. `$yummy[0]->meta_device_id`. Or, you could use `array_column` to grab an array of device ids for all devices returned from the API call, like this demo https://3v4l.org/ovAZa. Or do `foreach($yummy as $yum)` to gain access to each JSON object in the array. – Kim Hallberg Mar 18 '22 at 05:59

1 Answers1

1

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
Aqib Javed
  • 935
  • 1
  • 5
  • 15
  • Thanks for the reply . Yep this code is work thanks again. I have one more question. for $string how can use API url. I checked using below code but it's not working. '$string1 = 'http://123.231.44.147/nbro-invoker/api/sensor/flat-info'; $string = file_get_contents($string1); $yummy = json_decode($string); $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" } ' – Buddika Mar 18 '22 at 07:34
  • you can curl your api and pass result into $string. – Aqib Javed Mar 18 '22 at 07:36