Need help parsing JSON with PHP to build an URL -
i have json file looks this:
{ "name": "star_wars", "level": true, "condition": "hot", "actions": [ { "index": 0, "name": "sword_hit", "expectedmodelname": "swordmodel", "params": [ { "name": "language", "values": [ { "value": "gb" } ] }, { "name": "planet_version", "values": [ { "value": "hutta" } ] } ], "newparameters": [], "checks": [] }, { "index": 1, "name": "sword_bash", "expectedmodelname": "swordmodel2", "params": [ { "name": "language", "values": [ { "value": "it" } ] }, { "name": "planet_version", "values": [ { "value": "schnutta" } ] } ], "newparameters": [], "checks": [] } ]
i trying parse php , build urls out of it. each "action" url , parameters within "params" section. 2 urls should be:
index0: language=gb&planet_version=hutta
index1: language=it&planet_version=schnutta
my code looks far:
<?php $json = file_get_contents("data.json"); $jsoniterator = new recursiveiteratoriterator( new recursivearrayiterator(json_decode($json, true)), recursiveiteratoriterator::self_first); echo"http://www.myurl.de/test.php?"; foreach ($jsoniterator $key => $val) { if(is_array($val)) { echo "$key:\n"; } else { echo "$key=$val&"; } } ?>
it works designed not desired ;-) need "params" build url. how can implement condition, targets "name" , "values" "params" ?
please check simplistic solution:
$data = json_decode($json); $urls = array(); foreach($data->actions $k1=>$v1) { $urls[$k1] = ''; foreach($v1->params $k2=>$v2) { $urls[$k1].= $v2->name.'='.$v2->values[0]->value.'&'; } $urls[$k1] = rtrim($urls[$k1],'&'); } print_r($urls);
Comments
Post a Comment