简体   繁体   中英

How to decode JSON in a loop using PHP and json_decode?

I have json like this, returned from service:

{
    "reports" : {
        "name" : "reports"
        "response" : {
            "build" : {
                "version" : "2.55.10.0",
                "name" : "reports-app"
            }
        },
        "status" : 200
    },
    "static-application" : {
        "name" : "static-application"
        "response" : {
            "app" : {
                "name" : "client-frontend",
                "description" : "Client Static"
            },
            "build" : {
                "version" : "2.55.10.0",
                "name" : "client-frontend"
            }
        },
        "status" : 200
    },
    "static-help" : {
        "name" : "static-help"
        "response" : {
            "app" : {
                "name" : "client-frontend",
                "description" : "Client Static"
            },
            "build" : {
                "version" : "2.55.8.0",
                "name" : "client-frontend"
            }
        },
        "status" : 200
    }
}

And I'm trying to decode it with json_decode:

<?php

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 'https://domainname/api/aggregate/info');
    $json = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($json, true);
        echo $data['reports']['name']." | ".$data['reports']['response']['build']['version']."<br \>";
        echo $data['static-application']['name']." | ".$data['static-application']['response']['build']['version']."<br \>";
        echo $data['static-help']['name']." | ".$data['static-help']['response']['build']['version'];
?>

In return i have:

reports | 2.55.10.0
static-application | 2.55.10.0
static-help | 2.55.8.0

Everything works, but when the service will return more modules, I will have to manually add new echo sections.

How can I write the same in a loop?

You may use a foreach loop to iterate over all $data 's modules:

$data = json_decode($json, true);
foreach($data as $module => $data)
{
    if(isset($data[$module]['name']) && !empty($data[$module]['response']['build']['version']))
    {
        echo $data[$module]['name']." | ".$data[$module]['response']['build']['version']."<br \>";
    }
}

The foreach let you iterate a single element at a time, without the need to manually handle how many elements it contains.

The empty function checks for the presence of the key in the array and if it contains, in the case it may be missed or empty.

If you want to dynamically output the name and build version of the JSON's sections you can use a foreach() loop to iterate over all items:

$data = json_decode($json);

foreach ( $data as $key => $jsonData ) {
  echo $jsonData->name;
  echo " | ";
  echo $jsonData->response->build->version;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM