Sam Green
Sam Green

Reputation: 15

JSON PHP cant navigate through nested arrays

I'm trying to analyze some government data. Here's the JSON

 {
"results": [
    {
        "bill_id": "hres311-113",
        "bill_type": "hres",
        "chamber": "house",
        "committee_ids": [
            "HSHA"
        ],
        "congress": 113,
        "cosponsors_count": 9,
        "enacted_as": null,
        "history": {
            "active": false,
            "awaiting_signature": false,
            "enacted": false,
            "vetoed": false
 }

And here is the php

foreach($key['results'] as $get_key => $value){
  $bill_buff .= 'Bill ID: ' .   $value['bill_id'] . '<br/>';
  $bill_buff .= 'Bill Type: ' . $value['bill_type'] . '<br/>';
  $bill_buff .= 'Chamber: ' .   $value['chamber'] . '<br/>';
  $bill_buff .= 'Committee IDs: ' . $value['committee_ids'] . '<br/>';
  $bill_buff .= 'Congress: ' .  $value['congress'] . '<br/>';
  $bill_buff .= 'Cosponsor Count: ' .   $value['cosponsors_count'] . '<br/>';
  $bill_buff .= 'Enacted As: ' .    $value['enacted_as'] . '<br/>';
  $bill_buff .= 'History: {' . '<br/>';
  $history = $value['history'];
  $bill_buff .= 'Active: ' .    $history['active'] . '<br/>';
  $bill_buff .= 'Awaiting Signature: ' .    $history['awaiting_signature'] . '<br/>';
  $bill_buff .= 'Enacted: ' .   $history['enacted'] . '<br/>';
  $bill_buff .= 'Vetoed: ' .    $history['vetoed'] . '}<br/>';
}

It won't display History{Active, Awaiting Signature, Enacted, or Vetoed}. I've tried to do $value['history']['active'], as well as creating a variable to catch the info and then using that $catch['active'] but still can't get a result.

This has been annoying me for over a week now and I've looked long enough to decide I need to ask for help. Can anyone assist me?

P.S. I've also print_r($history) and go it to show me:

Array ( [active] => [awaiting_signature] => [enacted] => [vetoed] => )

Upvotes: 1

Views: 137

Answers (2)

cwallenpoole
cwallenpoole

Reputation: 82028

FALSE does not have a string value, which means it will not print in PHP. You'll not see it with echo, print or even fwrite(STDOUT....

You will, however, see all of that with var_dump.

var_dump($key['results']);

// outputs:
  array(1) {
    [0]=>
    array(8) {
      ["bill_id"]=>
      string(11) "hres311-113"
      ["bill_type"]=>
      string(4) "hres"
      ["chamber"]=>
      string(5) "house"
      ["committee_ids"]=>
      array(1) {
        [0]=>
        string(4) "HSHA"
      }
      ["congress"]=>
      int(113)
      ["cosponsors_count"]=>
      int(9)
      ["enacted_as"]=>
      NULL
      ["history"]=>
      array(4) {
        ["active"]=>
        bool(false)
        ["awaiting_signature"]=>
        bool(false)
        ["enacted"]=>
        bool(false)
        ["vetoed"]=>
        bool(false)
      }
    }
  }

Upvotes: 1

ernie
ernie

Reputation: 6356

The false is being treated as a boolean and not a string when you read in the value. PHP shows nothing when you try to echo out a boolean false (e.g. try a print false;). You could also verify this further by comparing the print_r output to var_dump output, e.g.:

Interactive shell

php > var_dump(false);
bool(false)
php > print_r(false);
php >

See this question for a possible solution: How to Convert Boolean to String

The basic overview is that you need to test the value, then output a string.

Upvotes: 1

Related Questions