0

Trying to parse the following data but having trouble getting the correct items to echo. What am I doing wrong?

{  
    "total_results":12585,
    "organizations":[  
      {  
         "ein":232669352,
         "strein":"23-2669352",
         "name":"AMERICAN HOME LIFE INTERNATIONAL INC",
         "sub_name":"AMERICAN HOME LIFE INTERNATIONAL INC",
         "city":"LANCASTER",
         "state":"PA",
         "ntee_code":"Q220",
         "raw_ntee_code":"Q220",
         "subseccd":3,
         "has_subseccd":true,
         "have_filings":true,
         "have_extracts":true,
         "have_pdfs":true,
         "score":417.01215
      },
      {  
         "ein":251691788,
         "strein":"25-1691788",
         "name":"HOME LIFE MINISTRIES",
         "sub_name":"HOME LIFE MINISTRIES",
         "city":"ANNVILLE",
         "state":"PA",
         "ntee_code":"X20",
         "raw_ntee_code":"X20",
         "subseccd":3,
         "has_subseccd":true,
         "have_filings":true,
         "have_extracts":true,
         "have_pdfs":true,
         "score":147.28578
      },
      {  
         "ein":240782824,
         "strein":"24-0782824",
         "name":"MIFFLINBURG AMERICAN LEGION HOME ASSOCIATION INC",
         "sub_name":"MIFFLINBURG AMERICAN LEGION HOME ASSOCIATION INC",
         "city":"MIFFLINBURG",
         "state":"PA",
         "ntee_code":null,
         "raw_ntee_code":null,
         "subseccd":7,
         "has_subseccd":true,
         "have_filings":true,
         "have_extracts":true,
         "have_pdfs":true,
         "score":145.12933
      },

Here is what I have for code so far:

<?php
    $url = "https://projects.propublica.org/nonprofits/api/v2/search.json?
    q=%22american%20home%20life%20international%20inc%22&state%5Bid%5D=PA";
    $json = file_get_contents($url);
    $json_data = json_decode($json, true);
    foreach ($json['organizations'] as $address)
    {
        echo "items:". $address['ein'] ."\n";
    };
?>
4
  • please read how to ask good questions. "having trouble" is too broad, also format json sample. Commented Feb 1, 2018 at 13:31
  • 1
    You're trying to loop over the string, rather than the $json_data variable. Pay attention to the warnings PHP raises - it's trying to help you. Commented Feb 1, 2018 at 13:36
  • foreach ($json_data['organizations'] as $address) Commented Feb 1, 2018 at 13:36
  • do select the answer if it helped you out Commented Feb 7, 2018 at 20:44

1 Answer 1

1

Change

foreach ($json['organizations'] as $address)

to

foreach ($json_data['organizations'] as $address)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.