For Loop does not work
-
First time developing in PHP. Used to coding in c#, so feel free to make fun of me. Here is the code I am using:
<?php
$response = file_get_contents($url);
$jsonResults = json_decode($response, true);echo "Title: ".$jsonResults['items']['0']['volumeInfo']['title']; //this works
echo "Title: ".$jsonResults['items']['1']['volumeInfo']['title']; //this works//this does not
for ($i = 0; $i < $jsonResults['items'].length; $i++) {
echo "Link: ".$jsonResults['items'][$i]['volumeInfo']['title'];
}
?>Now the json results look like this:
array(3) {
["items"]=>
array(10) {
[0]=>
array(7) {
["volumeInfo"]=>
array(12) {
["title"]=>
string(64) "MyTitle"
}***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
-
First time developing in PHP. Used to coding in c#, so feel free to make fun of me. Here is the code I am using:
<?php
$response = file_get_contents($url);
$jsonResults = json_decode($response, true);echo "Title: ".$jsonResults['items']['0']['volumeInfo']['title']; //this works
echo "Title: ".$jsonResults['items']['1']['volumeInfo']['title']; //this works//this does not
for ($i = 0; $i < $jsonResults['items'].length; $i++) {
echo "Link: ".$jsonResults['items'][$i]['volumeInfo']['title'];
}
?>Now the json results look like this:
array(3) {
["items"]=>
array(10) {
[0]=>
array(7) {
["volumeInfo"]=>
array(12) {
["title"]=>
string(64) "MyTitle"
}***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
PHP doesn't use the
.
operator for class members. If$jsonResults['items']
was an instance of a class, then the way to access thelength
member would be$jsonResults['items']->length
. Native arrays are not classes though, so to find their length you need to use thecount
function:<?php
$length = count($jsonResults['items']);
for($i = 0; $i < $length; $i++) {
echo "Link: ".$jsonResults['items'][$i]['volumeInfo']['title'];
}or you could use
foreach
instead offor
:<?php
foreach($jsonResults['items'] as $item) {
echo "Link: ".$item['volumeInfo']['title'];
} -
PHP doesn't use the
.
operator for class members. If$jsonResults['items']
was an instance of a class, then the way to access thelength
member would be$jsonResults['items']->length
. Native arrays are not classes though, so to find their length you need to use thecount
function:<?php
$length = count($jsonResults['items']);
for($i = 0; $i < $length; $i++) {
echo "Link: ".$jsonResults['items'][$i]['volumeInfo']['title'];
}or you could use
foreach
instead offor
:<?php
foreach($jsonResults['items'] as $item) {
echo "Link: ".$item['volumeInfo']['title'];
}I always
sizeof()
thouA hidden needle is way more effective than an unsheathed sword. That is, in the hand of professionals. What about you? Just pray your enemies are blind
-
I always
sizeof()
thouA hidden needle is way more effective than an unsheathed sword. That is, in the hand of professionals. What about you? Just pray your enemies are blind