Mayank pandey Mayank pandey - 2 months ago 10
JSON Question

Need help in php to parse key =>value in json when value again have nested key=>value?

I am new in php . I have json as below

{
"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": []
}


As you can see it has nested key=>value I need to parse each key even the nested one's with their values?

Answer

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;
    }
Comments