0

I have a JSON output I would like to convert to a PHP array. I tried with json_decode(), the problem is that there are arrays in the arrays. They are the first weapons with PHP and I have never used JSON. Can anyone help me?

Here is the JSON code:

{
    "a": "text",
    "b": "",

    "c": [

        {"name": "1", "id": "some text 1", "val": "x"},
        {"name": "2", "id": "some text 2", "val": "x"},
        {"name": "3", "id": "some text 3", "val": "x"}

    ]

}

I have to check that a variable is equal to name 1, contained in c, and if so, it also takes its id and val.

How can I do?

PS: I can compare two variables, but I do not know how to find name 1 and the corresponding data ..

5
  • 5
    Possible duplicate of Decode JSON to PHP Commented Jun 29, 2017 at 2:59
  • It's OK, you just decode it once, and all will be an array inside array too Commented Jun 29, 2017 at 3:00
  • How is the array c defined? Commented Jun 29, 2017 at 3:02
  • 1
    Loop through the c array and test the value of name. How is this complicated? Commented Jun 29, 2017 at 3:06
  • Sorry, I never worked with a array inside an array, now understood. Thank you all :) Commented Jun 29, 2017 at 3:11

1 Answer 1

1
$json = '{"a":"text","b":"","c":[{"name":"1","id":"some text 1","val":"x"},{"name":"2","id":"some text 2","val":"x"},{"name":"3","id":"some text 3","val":"x"}]}';

$json = json_decode($json,true);

echo $json["a"]."<br>";
echo $json["b"]."<br>";
echo $json["c"][1]["name"]."<br>";
echo "<pre>".print_r($json,true)."</pre>";
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.