-1

I'm decoding a json that I get from Notion's API in an associative array. When I var_dump it, it outputs as follows (with some information removed for security):

array(4) {
  ["object"]=>
  string(4) "list"
  ["results"]=>
  array(1) {
    [0]=>
    array(10) {
      ["object"]=>
      string(4) "page"
      ["id"]=>
      string(36) "dbid"
      ["created_time"]=>
      string(24) "2021-11-17T10:06:00.000Z"
      ["last_edited_time"]=>
      string(24) "2021-11-17T15:58:00.000Z"
      ["cover"]=>
      NULL
      ["icon"]=>
      NULL
      ["parent"]=>
      array(2) {
        ["type"]=>
        string(11) "database_id"
        ["database_id"]=>
        string(36) "dbid"
      }
      ["archived"]=>
      bool(false)
      ["properties"]=>
      array(2) {
        ["Meaning"]=>
        array(3) {
          ["id"]=>
          string(8) "%5Eny%3A"
          ["type"]=>
          string(9) "rich_text"
          ["rich_text"]=>
          array(1) {
            [0]=>
            array(5) {
              ["type"]=>
              string(4) "text"
              ["text"]=>
              array(2) {
                ["content"]=>
                string(26) "This is a really big house"
                ["link"]=>
                NULL
              }
              ["annotations"]=>
              array(6) {
                ["bold"]=>
                bool(false)
                ["italic"]=>
                bool(false)
                ["strikethrough"]=>
                bool(false)
                ["underline"]=>
                bool(false)
                ["code"]=>
                bool(false)
                ["color"]=>
                string(7) "default"
              }
              ["plain_text"]=>
              string(26) "This is a really big house"
              ["href"]=>
              NULL
            }
          }
        }
        ["DataElement"]=>
        array(3) {
          ["id"]=>
          string(5) "title"
          ["type"]=>
          string(5) "title"
          ["title"]=>
          array(1) {
            [0]=>
            array(5) {
              ["type"]=>
              string(4) "text"
              ["text"]=>
              array(2) {
                ["content"]=>
                string(8) "bigHouse"
                ["link"]=>
                NULL
              }
              ["annotations"]=>
              array(6) {
                ["bold"]=>
                bool(false)
                ["italic"]=>
                bool(false)
                ["strikethrough"]=>
                bool(false)
                ["underline"]=>
                bool(false)
                ["code"]=>
                bool(false)
                ["color"]=>
                string(7) "default"
              }
              ["plain_text"]=>
              string(8) "bigHouse"
              ["href"]=>
              NULL
            }
          }
        }
      }
      ["url"]=>
      string(63) "https://www.notion.so/bigHouse-connec"
    }
  }
  ["next_cursor"]=>
  NULL
  ["has_more"]=>
  bool(false)
}

I'm trying to get "This is a really big house" from the above associative array. I send the payload to notion that filters by the "DataElement" value "bigHouse". That works ok, but what I can't figure out is how to output "This is a really big house" from the associative array using PHP.

I have tried the following:

foreach($decoded as $key=>$val){
    echo $key." "."| Value: ".$val."<br>";
  }

This outputs:

object | Value: list
results | Value: Array
next_cursor | Value:
has_more | Value:

I'm not sure how to proceed from here. Any help would be appreciated. The name of the colum where "This is a really big house" is located on notion is: "Meaning".

Brendon
  • 29
  • 6
  • 1
    Can you please show that var_dump not as a one-liner, but so that we can easily see what the actual structure is? (Wrap it into a `pre` element, if it currently shows all in one line in the place where you output it.) – CBroe Nov 18 '21 at 08:41
  • 1
    Better format your json_decode result and you will get your answer. – agentofchaoss Nov 18 '21 at 08:41
  • Hi! Sorry about that. Forgot to add the better formatted value. I've updated it with the
     output. Thank you for pointing it out. ^_^
    – Brendon Nov 18 '21 at 08:48
  • 1
    the 'path' to your content looks to be: `$decoded['results'][0]['properties']['Meaning']['rich_text'][0]['text']['content']`. _Not tested_ – berend Nov 18 '21 at 09:14

1 Answers1

0

As @berend stated in the comments:

$decoded['results'][0]['properties']['Meaning']['rich_text'][0]['text']['content'];
DarkBee
  • 16,592
  • 6
  • 46
  • 58
Brendon
  • 29
  • 6