JSON response to single php variable -
json response is
[{"id":630770,"t2":"india a","t1":"south africa a"}, {"id":593454,"t2":"nottinghamshire","t1":"kent"}, {"id":593453,"t2":"northamptonshire","t1":"warwickshire"}, {"id":593457,"t2":"sussex","t1":"worcestershire"}, {"id":593451,"t2":"hampshire","t1":"derbyshire"}, {"id":593456,"t2":"surrey","t1":"durham"},{"id":593449,"t2":"essex","t1":"lancashire"}, {"id":593455,"t2":"somerset","t1":"gloucestershire"}, {"id":593452,"t2":"leicestershire","t1":"middlesex"}, {"id":593450,"t2":"glamorgan","t1":"yorkshire"}]
while using json decode in php, array is:
array ( [0] => stdclass object ( [id] => 630770 [t2] => india [t1] => south africa ) [1] => stdclass object ( [id] => 593454 [t2] => nottinghamshire [t1] => kent ) [2] => stdclass object ( [id] => 593453 [t2] => northamptonshire [t1] => warwickshire ) [3] => stdclass object ( [id] => 593457 [t2] => sussex [t1] => worcestershire ) [4] => stdclass object ( [id] => 593451 [t2] => hampshire [t1] => derbyshire ) [5] => stdclass object ( [id] => 593456 [t2] => surrey [t1] => durham ) [6] => stdclass object ( [id] => 593449 [t2] => essex [t1] => lancashire ) [7] => stdclass object ( [id] => 593455 [t2] => somerset [t1] => gloucestershire ) [8] => stdclass object ( [id] => 593452 [t2] => leicestershire [t1] => middlesex ) [9] => stdclass object ( [id] => 593450 [t2] => glamorgan [t1] => yorkshire ) )
i want result in singal variable instead of array as
$var = "on going matches $n : $t1 vs $t2\n";
it should return
on going matches 1: south africa vs india 2: kent vs nottinghamshire 3: warwickshire vs northamptonshire 4: worcestershire vs sussex 5: derbyshire vs hampshire 6: durham vs surrey 7: lancashire vs essex 8: gloucestershire vs somerset 9: middlesex vs leicestershire 10: yorkshire vs glamorgan
i using foreach loop output
on going matches 1: south africa vs india on going matches 2: kent vs nottinghamshire on going matches 3: warwickshire vs northamptonshire on going matches 4: worcestershire vs sussex on going matches 5: derbyshire vs hampshire on going matches 6: durham vs surrey on going matches 7: lancashire vs essex on going matches 8: gloucestershire vs somerset on going matches 9: middlesex vs leicestershire on going matches 10: yorkshire vs glamorgan
how can please me........
this should work:
$json = <<< eof [{"id":630770,"t2":"india a","t1":"south africa a"}, {"id":593454,"t2":"nottinghamshire","t1":"kent"}, {"id":593453,"t2":"northamptonshire","t1":"warwickshire"}, {"id":593457,"t2":"sussex","t1":"worcestershire"}, {"id":593451,"t2":"hampshire","t1":"derbyshire"}, {"id":593456,"t2":"surrey","t1":"durham"},{"id":593449,"t2":"essex","t1":"lancashire"}, {"id":593455,"t2":"somerset","t1":"gloucestershire"}, {"id":593452,"t2":"leicestershire","t1":"middlesex"}, {"id":593450,"t2":"glamorgan","t1":"yorkshire"}] eof; $data = "on going matches\n" . implode("\n", array_map(function($m){static $i=1; return $i++.': '.$m['t1'].' vs '.$m['t2'];}, json_decode($json, true)) ); echo $data."\n";
output:
on going matches 1: south africa vs india 2: kent vs nottinghamshire 3: warwickshire vs northamptonshire 4: worcestershire vs sussex 5: derbyshire vs hampshire 6: durham vs surrey 7: lancashire vs essex 8: gloucestershire vs somerset 9: middlesex vs leicestershire 10: yorkshire vs glamorgan
Comments
Post a Comment