Question

I have a json object like like this

[
  [
    {
      "class":"com.ehealth.data.Sample_Data",
      "collectedBy":"2013-07-21",
      "collectedDate":"Kamal",
      "orderID":2,
      "sampleID":2.897033553E9
    },
    {
      "class":"com.ehealth.data.Order_Data",
      "doctorUsername":"Kamal",
      "dueDate":"2014-01-02",
      "orderDate":"2013-12-12",
      "orderID":2,
      "patientID":"P0001",
      "prority":1,
      "status":"complete",
      "testType":"Fasting Blood Sugar"
    }
  ],
  [
    {
      "class":"com.ehealth.data.Sample_Data",
      "collectedBy":"2013-07-22",
      "collectedDate":"Kamal",
      "orderID":3,
      "sampleID":5.978956192E9
    },
    {
      "class":"com.ehealth.data.Order_Data",
      "doctorUsername":"Kamal",
      "dueDate":"2014-01-02",
      "orderDate":"2013-12-12",
      "orderID":3,
      "patientID":"P0001",
      "prority":2,
      "status":"complete",
      "testType":"Fasting Blood Sugar"
    }
  ]
]

and i decode it to php array

$data['query'] = json_decode($curl_response);

i try to access valuse like this

foreach ($query as $row) {
    echo $row->sampleID;           
}

but when i am going to access the the values inside it i can not access i get a error

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object 

can anybody help me. i dont know what to do

Was it helpful?

Solution

You can get it:

foreach ($query as $row) {
   echo $row[0]->sampleID;           
}

OTHER TIPS

<?php
$str = '[[{"class":"com.ehealth.data.Sample_Data","collectedBy":"2013-07-21","collectedDate":"Kamal","orderID":2,"sampleID":2.897033553E9},{"class":"com.ehealth.data.Order_Data","doctorUsername":"Kamal","dueDate":"2014-01-02","orderDate":"2013-12-12","orderID":2,"patientID":"P0001","prority":1,"status":"complete","testType":"Fasting Blood Sugar"}],[{"class":"com.ehealth.data.Sample_Data","collectedBy":"2013-07-22","collectedDate":"Kamal","orderID":3,"sampleID":5.978956192E9},{"class":"com.ehealth.data.Order_Data","doctorUsername":"Kamal","dueDate":"2014-01-02","orderDate":"2013-12-12","orderID":3,"patientID":"P0001","prority":2,"status":"complete","testType":"Fasting Blood Sugar"}]]';

$json = json_decode($str,true);

foreach ($json as $datas){
    foreach ($datas as $data){
        echo (isset($data['sampleID']) ? $data['sampleID'] :'')."<br/>";
    }
}

echo '<pre>';
print_r ($json);
echo '</pre>';
// exit;

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top