I cURL to a page which produces an array of json of the fashion:
[{"id":1,"name":"Drew"},{"id":2,"name":"Joyce"},{"id":3,"name":"Vick"}]
I use $result = json_decode($json, true)
to get back an associative array.
I am having trouble though iterating through the array though and accessing individual pieces of each json object like the name field. Is there an easy way to accomplish this?
Another issue is, I was planning to use a foreach
loop as I might not know the exact length of the array of json objects, is there a way to incorporate the solution to my first issue into a for each loop or other structure that might take in a variable sized array and stop when it reaches the end?
This should be fairly simple...
foreach ($result as $item) {
echo 'id: ', $item['id'],
', name: ', $item['name'],
PHP_EOL;
}
Demo here - http://ideone.com/xwy2zH
Whilst developing, I also highly recommend you enable full error reporting. Set these properties in your development environment's php.ini
file then restart your web server
error_reporting = E_ALL
display_errors = On