Reputation: 26
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 ..
Upvotes: 0
Views: 52
Reputation: 309
$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>";
Upvotes: 1