Can any one tell me how to iterate below given JSON data using PHP, { "value1":"3", "value 2":"5", "Id":[ "210", "211" ] }
6
-
try json_decode() functionSathiya saravana Babu– Sathiya saravana Babu2016-09-17 05:27:54 +00:00Commented Sep 17, 2016 at 5:27
-
Example Code is shown in below <?php $json = '{"foo-bar": 12345}'; $obj = json_decode($json); print $obj->{'foo-bar'}; // 12345 ?>Sathiya saravana Babu– Sathiya saravana Babu2016-09-17 05:28:52 +00:00Commented Sep 17, 2016 at 5:28
-
i was using array also , how could be do this.parthi– parthi2016-09-17 05:30:14 +00:00Commented Sep 17, 2016 at 5:30
-
I have answer your question. Let me know if any issues. Could you please explain your needed solution properly that will be helpful to get an immediate solutionSathiya saravana Babu– Sathiya saravana Babu2016-09-17 05:41:52 +00:00Commented Sep 17, 2016 at 5:41
-
Thank you Mr Sathiaya , i have done it..parthi– parthi2016-09-17 06:04:30 +00:00Commented Sep 17, 2016 at 6:04
|
Show 1 more comment
3 Answers
<?php $json='{ "value1":"3", "value 2":"5", "Id":[ "210", "211" ] }';
$getarr=json_decode($json,true);
foreach( $getarr as $key => $value)
{
if(is_array($value))
{
// ID has an array so retrieve a value
foreach($value as $key1 => $value1)
{
echo $key."=>".$value1;
}
}else
{
echo $key."=>".$value;
}
} ?>
It will be produce the below output. Hope it will be helpful to solve your problem
Answer:
value1=>3 value 2=>5 Id=>210 Id=>211
Comments
Copy and paste you can change the print out to fit your use
<?php
$bar=' { "value1":"3", "value 2":"5", "Id":[ "210", "211" ] }';
//var_dump($bar);
$JSONdata = json_decode($bar, true);
//echo
$JSONdataValue="";
$JSONdataValue.=$JSONdata['value1']."<br>";
$JSONdataValue.=$JSONdata['value 2']."<br>";
$JSONdataValue.=$JSONdata['Id'][0]."<br>";
$JSONdataValue.=$JSONdata['Id'][1]."<br>";
echo $JSONdataValue;
?>