Skip to content
Advertisement

PHP Looping through JSON data without knowing the numbers of data

I am trying to get all images from an image API. It returns a maximum of 500 result at a time and if the result has a next_page field, then I have to grab the value of that field and add it to the URL. The code should continue looping until that field is absent. I used the following code to grab the first two pages:

$key = true;
$link = 'https://url.com/dhj/?prefix=images&type=download';
$json = file_get_contents($link);
$data = json_decode($json, true);
$dataArray = array();
foreach ($data["images"] as $r)
{
    array_push($dataArray, array($r["id"], $r["image"]));
}
while($key)
{
    if($data["next_page"])
    {
        $key=true;
        $link2 = "https://url.com/dhj/?prefix=images&type=download&next_page=" . $data[$next_page];
        $json2 = file_get_contents($link2);
        $data2 = json_decode($json2, true);
        foreach ($data2["images"] as $r2)
        {
            array_push($dataArray, array($r2["id"], $r2["image"]));
        }
    }
    else
    {
        $key=false;
    }
}

This should fetch 2000 records but is only fetching 1000 records, so it appears the loop is not working as expected.

Advertisement

Answer

So your problem is that you are only fetching twice. The second time, you never check $data2 for a next page, so everything stops. You do not want to keep going like this, or you will need $data3, $data4, etc.

A do/while loop is similar to a while loop, except that it always runs at least once. The condition is evaluated at the end of the loop instead of the beginning. You can use that behaviour to ensure you always get the first page of data, and then use the condition to check if you should keep getting more.

$page = "";
do {
    $link = "https://url.com/dhj/?prefix=images&type=download&next_page=$page";
    $json = file_get_contents($link);
    $data = json_decode($json, true);
    foreach ($data["images"] as $r) {
        $dataArray[] = [$r["id"], $r["image"]];
    }
    $page = $data["next_page"] ?? "";
} while ($page);

Note I’ve got rid of your array_push() call. This is rarely used in PHP because the $var[] syntax is less verbose and doesn’t require predeclaration of the array. Likewise, calls to array() have long been replaced by use of array literal syntax. The expression $page = $data["next_page"] ?? "" uses the null coalesce operator, and is identical to:

if (isset($data["next_page"])) {
    $page = $data["next_page"];
} else {
    $page = "";
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement