code333 Posted June 2, 2010 Share Posted June 2, 2010 Below is an array and its elements can be accessed using a foreach loop but when I want to access its elements with normal for loop it does not work? any ideas? $bucket_contents = $s3->getBucket("gmt.dvd.052110"); foreach ($bucket_contents as $file){ $fname = $file['name']; $furl = "http://s3.amazonaws.com/gmt.dvd.052110/".$fname; //output a link to the file echo "<a href=\"$furl\">$fname</a><br />"; } //ABOVE WORKS FINE //BELOW HAS NO OUTPUT: $bucket_contents = $s3->getBucket("gmt.dvd.052110"); for ($i = 0; $i < sizeof($bucket_contents); $i++){ echo $bucket_contents[$i]; } If $bucket_contents is an array why can't I just do an old fashion for loop? Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/203585-array-not-working/ Share on other sites More sharing options...
kenrbnsn Posted June 2, 2010 Share Posted June 2, 2010 Because the array you're trying to access does not have numerical indices. Do a <?php echo '<pre>' . print_r($bucket_contents,true) . '</pre>'; ?> and you'll see this. Ken Link to comment https://forums.phpfreaks.com/topic/203585-array-not-working/#findComment-1066426 Share on other sites More sharing options...
code333 Posted June 2, 2010 Author Share Posted June 2, 2010 I see but then should this not work: echo $bucket_contents['name']; I don't get anything.. Link to comment https://forums.phpfreaks.com/topic/203585-array-not-working/#findComment-1066431 Share on other sites More sharing options...
kenrbnsn Posted June 2, 2010 Share Posted June 2, 2010 Please post the output of the print_r function I gave you. Ken Link to comment https://forums.phpfreaks.com/topic/203585-array-not-working/#findComment-1066432 Share on other sites More sharing options...
code333 Posted June 2, 2010 Author Share Posted June 2, 2010 Array ( [CagedDVD.flv] => Array ( [name] => CagedDVD.flv [time] => 1275422377 => 400148528 [hash] => d850618ffe31f68e4bad4353ee71ed0b ) [CagedDVD.mov.red.zip] => Array ( [name] => CagedDVD.mov.red.zip [time] => 1275086872 => 602661608 [hash] => 73355b6de20ec1437f5e5f15b23e21c7 ) [CagedDVD.mov.zip] => Array ( [name] => CagedDVD.mov.zip [time] => 1275070295 => 602661608 [hash] => 73355b6de20ec1437f5e5f15b23e21c7 ) [ModesDVD.mov.red.zip] => Array ( [name] => ModesDVD.mov.red.zip [time] => 1275086624 => 706116261 [hash] => 8f4fe233e9a21c423c3477df1211cdab ) [ModesDVD.mov.zip] => Array ( [name] => ModesDVD.mov.zip [time] => 1274843456 => 1426863264 [hash] => 4c4f05ac40e869ec80e126e74d7fb87b ) [PentatonicDVD.mov.red.zip] => Array ( [name] => PentatonicDVD.mov.red.zip [time] => 1275095016 => 618509462 [hash] => 061af76d99402ec7beabba50cade8060 ) [PentatonicDVD.mov.zip] => Array ( [name] => PentatonicDVD.mov.zip [time] => 1274474503 => 939287499 [hash] => 80aaebf2deb719d8a107d2df3032faf2 ) [ProgressionsDVD.flv] => Array ( [name] => ProgressionsDVD.flv [time] => 1275423397 => 1218201159 [hash] => c1aa5e751bbcf2928b1b4ee10d6e1b9c ) [ProgressionsDVD.mov.red.zip] => Array ( [name] => ProgressionsDVD.mov.red.zip [time] => 1275108754 => 771554647 [hash] => 976747b563808f196c40a6e9df2cdc82 ) [ProgressionsDVD.mov.zip] => Array ( [name] => ProgressionsDVD.mov.zip [time] => 1274841400 => 1580149160 [hash] => c3a377580848ca9edb40e10a83dd5e81 ) ) Link to comment https://forums.phpfreaks.com/topic/203585-array-not-working/#findComment-1066433 Share on other sites More sharing options...
codebyren Posted June 2, 2010 Share Posted June 2, 2010 As kenrbnsn said, it comes down to the indices: echo $bucket_contents['name']; ...will be invalid while echo $bucket_contents['CagedDVD.flv']['name']; // or echo $bucket_contents['CagedDVD.mov.red.zip']['name']; // etc. ... should work fine. There is that extra level to the array that you are missing. This should also show why $bucket_contents[$i] doesn't work: $i in your example is an incrementing number and there are no numeric indices in the array. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/203585-array-not-working/#findComment-1066442 Share on other sites More sharing options...
code333 Posted June 2, 2010 Author Share Posted June 2, 2010 I see. it is because it is a multi-dimensional array. This sheds light to my confusion. so when this is expressed: foreach ($bucket_contents as $file) $bucket_contents is accessing its own sub group of arrays, correct? Link to comment https://forums.phpfreaks.com/topic/203585-array-not-working/#findComment-1066459 Share on other sites More sharing options...
codebyren Posted June 2, 2010 Share Posted June 2, 2010 Yes, when you say: foreach ($bucket_contents as $file) ... you are effectively returning each top-level array in $bucket_contents (as $file). So the foreach loop basically does: $file = $bucket_contents['CagedDVD.flv']; // this is an array containing 'name', 'time', size' and 'hash' $file = $bucket_contents['CagedDVD.mov.red.zip']; $file = $bucket_contents['CagedDVD.mov.zip']; // ... // .. // etc. Except you obviously get to do something with the current state of $file during the foreach loop before it is re-assigned the next array in $bucket_contents. Hope this clears it up... Link to comment https://forums.phpfreaks.com/topic/203585-array-not-working/#findComment-1066469 Share on other sites More sharing options...
code333 Posted June 2, 2010 Author Share Posted June 2, 2010 I see so it is literally a pointer to the top level array with access to its sub arrays. very interesting.. Thanks a bunch I learned something new today about programming. Thanks again!! Link to comment https://forums.phpfreaks.com/topic/203585-array-not-working/#findComment-1066479 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.