0

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() function Commented 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 ?> Commented Sep 17, 2016 at 5:28
  • i was using array also , how could be do this. Commented 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 solution Commented Sep 17, 2016 at 5:41
  • Thank you Mr Sathiaya , i have done it.. Commented Sep 17, 2016 at 6:04

3 Answers 3

1
<?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

Sign up to request clarification or add additional context in comments.

Comments

0

Use json_decode function for converting it to array.

$array = json_decode( $json, true );

Comments

0

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;
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.