-2

I need to iterate some JSON formatted data into loop using PHP. My JSON data is below:

{  
   "question1":{  
      "ques":"questin1",
      "optional":[  
         {  
            "opt":"option1"
         },
         {  
            "opt":"option2"
         }
      ]
   },
   "question2":{  
      "ques":"questin2",
      "optional":[  
         {  
            "opt":"option1"
         },
         {  
            "opt":"option2"
         }
      ]
   }
}

I need to run the loop so that the result data will come in above format using PHP.

9
  • So you just want to print out your json?! Or what does "above format" mean? Commented Jun 27, 2016 at 10:41
  • 2
    Did you try json_decode() ? Commented Jun 27, 2016 at 10:41
  • @JimmyKo : Actually those json format is not my input but it should be the output after loop has completed. Commented Jun 27, 2016 at 10:47
  • Then show your current data and loop Commented Jun 27, 2016 at 10:48
  • i can use those data which are present in that json .its should be total printed output. Commented Jun 27, 2016 at 10:49

2 Answers 2

0

Convert the php object to json object using json_encode

// convert object => json
$json = json_encode($myObject);

This might helpful: https://stackoverflow.com/a/9858457/6285410

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

Comments

-1

What you showed us is a Possible JSON Data. In this Format, we can do nothing with it in PHP except by decoding back into Native PHP Object. Once that is done, You can access all the Properties of the Object like you do with normal PHP Object like $objData->questin1. Here's what is meant with the above statements:

    <?php

        $strJson = '{
           "question1":{
              "ques":"questin1",
              "optional":[
                 {
                    "opt":"option1"
                 },
                 {
                    "opt":"option2"
                 }
              ]
           },
           "question2":{
              "ques":"questin2",
              "optional":[
                 {
                    "opt":"option1"
                 },
                 {
                    "opt":"option2"
                 }
              ]
           }
        }';

        $objData    = json_decode($strJson);
        var_dump($objData);


        // NOW, TO GET AT EACH OF THE PROPERTIES OF THE OBJECT IS EASY...

        // ACCESS THE question1 OR question2
        $q1     = $objData->question1;
        $q2     = $objData->question2;

        // ACCESS THE que WITHIN question 1 OR question2
        $k1     = $objData->question1->ques;        //  EQUIVALENT TO: $q1->ques
        $k2     = $objData->question2->ques;        //  EQUIVALENT TO: $q2->ques

        // ACCESS THE optional ARRAY INSIDE OF question 1 OR question2
        $opt1   = $objData->question1->optional;    //  EQUIVALENT TO: $q1->optional
        $opt2   = $objData->question2->optional;    //  EQUIVALENT TO: $q2->optional

        var_dump($q1, $q2, $k1, $k2, $opt1, $opt2);


    ?>

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.