skip to Main Content

I can’t get php to list each item from the arrays with

foreach($decodedData->data->array as $values)
    {
         echo $values->title . "n";

While I’m trying it to list as such (I know my code isn’t setup to list each variable, but I cant even list the title with my current code):
Title 1, ID, URL-Title 2, ID, URL-Title 3, ID, URL

$decodedData array:

array(2) {
  ["data"]=>
  array(10) {
    [0]=>
    array(1) {
      ["node"]=>
      array(3) {
        ["id"]=>
        int(20)
        ["title"]=>
        string(6) "title"
        ["main_picture"]=>
        array(2) {
          ["medium"]=>
          string(57) "url"
          ["large"]=>
          string(58) "url"
        }
      }
    }
    [1]=>
    array(1) {
      ["node"]=>
      array(3) {
        ["id"]=>
        int(42249)
        ["title"]=>
        string(15) "title"
        ["main_picture"]=>
        array(2) {
          ["medium"]=>
          string(60) "url"
          ["large"]=>
          string(61) "url"
        }
      }
    }
    [2]=>
    array(1) {
      ["node"]=>
      array(3) {
        ["id"]=>
        int(44511)
        ["title"]=>
        string(12) "title"
        ["main_picture"]=>
        array(2) {
          ["medium"]=>
          string(60) "url"
          ["large"]=>
          string(61) "url"
        }
      }
    }

The relevant code:

    // Decoding JSON data
    $decodedData =
        json_decode($result, true);
    // Decoded form
    var_dump($decodedData);
    foreach($decodedData->data->array as $values)
    {
         echo $values->title . "n";
    }

2

Answers


  1. The second parameter of json_decode($json, $associative) controls whether you will get an object (if set to FALSE) or an associative array (if set to TRUE).

    from the PHP docs: https://www.php.net/manual/en/function.json-decode.php

    associative
    When true, JSON objects will be returned as associative arrays; when false, JSON objects will be returned as objects. When
    null, JSON objects will be returned as associative arrays or objects
    depending on whether JSON_OBJECT_AS_ARRAY is set in the flags.*

    So you should either pass FALSE to the second parameter to receive a list of objects or to update your code to work with a regular array:

    foreach($decodedData['data'] as $values) {
        echo $values['node']['title'] . "n";
    }
    
    Login or Signup to reply.
  2. $decodedData->data doesn’t have a property called "array" so ->array makes no sense. The data item is itself an array, so you can loop through that.

    Also there are no objects here at all so there should be no attempts to access object properties using ->. You have decoded the JSON in associative array format (by using true as the second argument to json_decode).

    And if you look at the data structure more closely, you’ll also see that "title" is actually under another layer with a key named "node".

    So I expect that

    foreach ($decodedData["data"] as $item) { 
      echo $item["node"]["title"] . "n";
    }
    

    should do the trick.

    Background reading: How to extract and access data from JSON with PHP? and How can I access an array/object?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search