It's pretty straightforward to convert JSON to arrays in PHP.
But first, your JSON file is missing a curly brace.
Also, there is a process of converting JSON string to PHP usable datatypes.
The first thing is to decode the JSON string using json_decode().
This function returns an appropriate PHP type, in this case, an object.
To access an object's data you use ->
as shown in the code
"results":{
"todo":[
"Buy 1 Macaroni",
"Eat",
"Praying",
"Sleep"
]
}
}
To convert this, you can simply do :
$json = '{
"results":{
"todo":[
"Buy 1 Macaroni",
"Eat",
"Praying",
"Sleep"
]
}
}';
$todos = json_decode($json);
var_dump($todos->results->todo);
The output is :
array(4) {
[0]=>
string(14) "Buy 1 Macaroni"
[1]=>
string(3) "Eat"
[2]=>
string(7) "Praying"
[3]=>
string(5) "Sleep"
If you'd like to access each element of an array using a loop instead of var_dump, then do the following
$json = '{
"results":{
"todo":[
"Buy 1 Macaroni",
"Eat",
"Praying",
"Sleep"
]
}
}';
$todos = json_decode($json);
foreach($todos->results->todo as $todo )
{
echo $todo."\n";
}
Output is
Buy 1 Macaroni
Eat
Praying
Sleep