2

I have this response from json_decode()

// Decode JSON into PHP array
$response_data = json_decode($curl_data, true);

$user_data = $response_data;
var_dump($user_data);

Response :

array(3) { ["Items"]=> array(1) { [0]=> array(3) { ["date"]=> string(9) "4/12/2022" ["ongoing"]=> string(1) "1" ["id"]=> string(1) "1" } } ["Count"]=> int(1) ["ScannedCount"]=> int(1) }

now I just wanted to get the ongoing part so I did it like this

if(!empty($user_data))
{
  foreach($user_data as $user)
  {
    var_dump($user[0]->ongoing);
  }
}

but it gives me an error

enter image description here

$user_data has the response above. Why is it giving me an error like that

now I have a different error when I tried this

// Decode JSON into PHP array
$response_data = json_decode($curl_data, true);

// Print all data if needed
// print_r($response_data);
// die();

// All user data exists in 'data' object
$user_data = $response_data;

if(!empty($user_data))
{
  foreach($user_data as $user)
  {
    var_dump($user[0]['ongoing']);
  }
}

enter image description here

JustANoob
  • 67
  • 5
  • `$user_data` is an array, not an object. You must have told [json_decode](https://www.php.net/manual/en/function.json-decode.php) to create an associated array. – aynber Apr 12 '22 at 16:21
  • In your loop your handle your item as object. `var_dump($user[0]->ongoing);` but you pass to the decode function the second parameter true. Then you have access like that: `$user[0]['ongoing']` – Maik Lowrey Apr 12 '22 at 16:29
  • 1
    Look carefully at what your `var_dump` is telling you. Don't just glance at the bit you're interested in, follow it through from the outside in, looking at how the arrays nest inside each other. Think of it like a maze, where you have to describe every turn to get from where you are to where you want to go; you can't just skip passed turnings. – IMSoP Apr 12 '22 at 16:43
  • As IMSoP said, look very very carefully at your result. `$user_data` has 3 properties: Items, Count, and ScannedCount. Items is an array of arrays. When you iterate through user_data, the first time is fine because it's accessing Items, which contains an array with a single value, which has the key `ongoing`. But the next time through, it's Count, then it's ScannedCount – aynber Apr 12 '22 at 16:52

1 Answers1

2

In your loop your handle your item as object. var_dump($user[0]->ongoing); but you pass to the decode function the second parameter true. Then you return an array. Afterwards you have access like that: $user[0]['ongoing'] or you pass to the loop directly the items. See second example. Thank @IMSoP for the right hint!

Update

The problem is that you have item, count and ScannedCount as key. But only item has the property ongoing. You need a condition to check if the key ongoingexists. Then it works.

version 1 (with key check)

$str = '{"Items":[{"date":"4/12/2022","ongoing":"1","id":"1"}],"Count":1,"ScannedCount":1}';
$user_data = json_decode($str, true);

if (! empty($user_data)) {
    foreach($user_data as $key => $value) {
        if ($key === 'Items') {
           print_r($value[0]['ongoing']);   
        }
        
    }   
}

Version 2 (pass items directly to loop)

$str = '{"Items":[{"date":"4/12/2022","ongoing":"1","id":"1"}],"Count":1,"ScannedCount":1}';
$user_data = json_decode($str, true);

if (! empty($user_data)) {
    foreach ($user_data['items'] as $value) {
       print_r($value[0]['ongoing']);           
    }   
}
Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79