skip to Main Content

I’m not all that familiar with JSON, much less trying to pull values from it into PHP, so I’m running into a bit of headache when trying to do so.

If this weren’t a nested/multidimensional array, this would be a simple search, copy, and paste job, but the problem I’m running into is that I’m only able to pull the first result from the array. Granted my code is a bit of a mess because I’ve been trying new methods and hacking away at it to try to get the intended result, but I’m not sure how to process the data in a way that pulls just the information I’m looking for.

I greatly appreciate any tips/advice you might be able to give to guide me to pulling all of the results instead of just the first. I’m sure it’s something simple I’m just not seeing, but I’ve been at this for a while and finally decided I’d ask for some assistance.

Here’s the PHP coding I’m using at the moment, which is only pulling one result:

$url = "https://somewhere.com/give-api/v1/donors/?key=***&token=***&number=999";
$data = file_get_contents($url);
$donors = json_decode($data, true);
$count = 0;
$output = array();

foreach ($donors as $donor) {
  $spent = substr($donor[$count]['stats']['total_spent'], 0, strpos($donor[$count]['stats']['total_spent'], "."));
  if ($spent != 0)
  {
    $level = check_spent($spent);
    //       ^ Custom function, just returns a level number based on the $spent value
    $output += [$spent, $level, $donor[$count]['info']['first_name'] . " " . $donor[$count]['info']['last_name']];
  }
  $count++;
}

echo '<pre>';
print_r($output);
echo '</pre>';

Here’s an anonymized version of the data:

{
"donors": [
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "8",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "[email protected]"
        },
        "stats": {
            "total_donations": "1",
            "total_spent": "40.000000"
        },
        "address": {
            "billing": [
                {
                    "line1": "1234 Some Pl",
                    "line2": "",
                    "city": "Somewhere",
                    "state": "DC",
                    "country": "US",
                    "zip": "12345"
                }
            ]
        }
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "7",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "[email protected]"
        },
        "stats": {
            "total_donations": "1",
            "total_spent": "50.000000"
        },
        "address": {
            "billing": [
                {
                    "line1": "1234 Some Pl",
                    "line2": "",
                    "city": "Somewhere",
                    "state": "DC",
                    "country": "US",
                    "zip": "12345"
                }
            ]
        }
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "6",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "[email protected]"
        },
        "stats": {
            "total_donations": "1",
            "total_spent": "100.000000"
        },
        "address": {
            "billing": [
                {
                    "line1": "1234 Some Pl",
                    "line2": "",
                    "city": "Somewhere",
                    "state": "DC",
                    "country": "US",
                    "zip": "12345"
                }
            ]
        }
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "5",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "[email protected]"
        },
        "stats": {
            "total_donations": "0",
            "total_spent": "0.000000"
        },
        "address": []
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "4",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "[email protected]"
        },
        "stats": {
            "total_donations": "0",
            "total_spent": "0.000000"
        },
        "address": []
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "3",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "[email protected]"
        },
        "stats": {
            "total_donations": "1",
            "total_spent": "50.000000"
        },
        "address": {
            "billing": [
                {
                    "line1": "1234 Some Pl",
                    "line2": "",
                    "city": "Somewhere",
                    "state": "DC",
                    "country": "US",
                    "zip": "12345"
                }
            ]
        }
    },
    {
        "info": {
            "user_id": "",
            "username": "",
            "display_name": "",
            "donor_id": "2",
            "title_prefix": "",
            "first_name": "John",
            "last_name": "Doe",
            "email": "[email protected]"
        },
        "stats": {
            "total_donations": "1",
            "total_spent": "50.000000"
        },
        "address": {
            "billing": [
                {
                    "line1": "1234 Some Pl",
                    "line2": "",
                    "city": "Somewhere",
                    "state": "DC",
                    "country": "US",
                    "zip": "12345"
                }
            ]
        }
    }
],
"request_speed": 0.0026030540466308594
}

2

Answers


  1. $donors is the whole map there, $donors[“donors”] is where you need to iterate

    Login or Signup to reply.
  2. Have a look at the data you get after json_decode, you will probably get an array with only one key: donors.

    print_r($donors);
    

    If so, do this instead when iterating:

    foreach ($donors['donors'] as $donor) {
      // do your work
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search