1

i have the following JSON:

{"Switches":["Auswahl1","Auswahl2","Auswahl3"],"Check_MK":["Auswahl1","Auswahl2","Auswahl3"],"Testgroup":["Auswahl1","Auswahl2","Auswahl3"],"Printer":["Auswahl1","Auswahl2","Auswahl3"],"CAD":["Auswahl1","Auswahl2","Auswahl3"]}

How do i loop each object while using PHP?

My thoughts were the following:

   <?php

      $jsonfile = file_get_contents('tags.json');
      echo $jsonfile . "<br><br>";
      $decode = json_decode($jsonfile);

      foreach($decode as $key => $value) {
        echo $key . $value;
      }

    ?>

Doesn't work..... Also

echo $decode[1];

and

echo $decode[1][1];

doesn't work..

0

2 Answers 2

2

You need to add a second parameter to json_decode()

This parameter returns associative array instead of existing object (if existing).

$decode = json_decode($jsonfile, TRUE);

This will convert your JSON decoded data into associative array.

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

Comments

1
$jsonfile = file_get_contents('tags.json');
echo $jsonfile . "<br><br>";
$decode = json_decode($jsonfile);    

now $decode is equivalent to:

$decode = new stdClass();
$decode->Switches = array();
$decode->Switches[] = "Auswahl1";
$decode->Switches[] = "Auswahl2";
$decode->Switches[] = "Auswahl3";
$decode->Check_MK = array();
...

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.