需要帮助php解析json中的key =>值,当值再次具有嵌套键=>值?

时间:2016-09-25 17:47:08

标签: php json

我是php新手。我有json如下

{
    "ext": {
        "output": "vast"
    },
    "cur": ["USD"],
    "allimps": 0,
    "tmax": 350,
    "regs": {},
    "imp": [{
        "ext": {
            "viewabilitymeasurability": 2
        },
        "bidfloor": 0.02,
        "bidfloorcur": "USD",
        "id": "1",
        "video": {
            "boxingallowed": 1,
            "ext": {
                "skippability": 1,
                "incentivized": 2,
                "clickability": 1,
                "instream": 2
            },
            "sequence": 1,
            "protocol": 2,
            "linearity": 1,
            "maxduration": 30,
            "startdelay": 0,
            "playbackmethod": [2],
            "protocols": [2, 5],
            "minduration": 15,
            "mimes": ["video/mp4"]
        },
        "instl": 1
    }],
    "audienceSegment": 0,
    "site": {
        "ref": "http://imasdk.googleapis.com/js/core/bridge3.145.0_en.html",
        "domain": "imasdk.googleapis.com",
        "publisher": {
            "ext": {
                "direct": 2
            },
            "domain": "imasdk.googleapis.com",
            "id": "8678"
        },
        "id": "eba5e1348aaa8dbce4044cdb1e54141c"
    },
    "at": 2,
    "bcat": [],
    "id": "9892d4df-03da-4981-a4ca-7172fd615a25",
    "user": {
        "ext": {
            "AudienceData": []
        },
        "buyeruid": "7719d705c6abef5dc7ed6153dbcf963d"
    },
    "device": {
        "geo": {
            "country": "VNM",
            "city": "Hanoi",
            "lon": 105.85,
            "region": "VN-64",
            "type": 2,
            "lat": 21.0333
        },
        "ext": {},
        "carrier": "FPT Telecom Company",
        "ip": "1.55.255.255",
        "js": 0,
        "language": "en",
        "ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:48.0) Gecko/20100101 Firefox/48.0",
        "devicetype": 2
    },
    "badv": []
}

正如你所看到它有嵌套的key =>值我需要解析每个键,即使是嵌套的键还是它们的值?

1 个答案:

答案 0 :(得分:0)

function validateRequest($ arr,$ path){

foreach($arr as $key => $val) {
    if (is_array($val))
        validateRequest($val, ($path!=""?$path . ">" . $key:$key));

elseif(!is_numeric($ key)|| is_numeric($ key)){

        echo "<br />".($path!=""?$path.">".$key:$key)."=".$val;
    }