sinkhead Posted May 17, 2007 Share Posted May 17, 2007 $x=0; foreach ($list[$x]['filename'] as $value){ echo $value."<br />"; $x++; } $list is an array containing 2 parts. The first bit is an ID number. The second bit is the part of info for that ID number. I want to echo all the values of $list[$x]['filename'] where $x is an increasing number. The error I get is Warning: Invalid argument supplied for foreach() in /home/sinkhead/public_html/romnet/fctp/temp/78695/step2.php on line 46 Thanks - Sam Link to comment https://forums.phpfreaks.com/topic/51894-solved-foreach-help/ Share on other sites More sharing options...
Lumio Posted May 17, 2007 Share Posted May 17, 2007 <?php $x=0; for ($x=0; $x<count($list); $x++) { foreach ($list[$x] as $value){ echo $value."<br />"; } } ?> I hope that works Link to comment https://forums.phpfreaks.com/topic/51894-solved-foreach-help/#findComment-255805 Share on other sites More sharing options...
Orio Posted May 17, 2007 Share Posted May 17, 2007 Hmm, if I get the structure of your array correctly, one of these solutions would do: 1) <?php foreach ($list[$x] as $value) echo $value['filename']."<br />"; ?> 2) <?php for ($x = 0; $x < count($list); $x++) echo $list[$x]['filename']; ?> Orio. Link to comment https://forums.phpfreaks.com/topic/51894-solved-foreach-help/#findComment-255808 Share on other sites More sharing options...
sinkhead Posted May 17, 2007 Author Share Posted May 17, 2007 Hm. I'm getting the following outcome: shinji.gif aj421.png sinkhead.png s s o (Yes, with a big gap) When I use the code: <?php echo $list['0']['filename']."<br />"; echo $list['1']['filename']."<br />"; echo $list['2']['filename']."<br />"; $x = 0; foreach ($list[$x] as $value) { echo $value['filename']."<br />"; $x++; } ?> This is weird - Sam Link to comment https://forums.phpfreaks.com/topic/51894-solved-foreach-help/#findComment-255823 Share on other sites More sharing options...
chigley Posted May 17, 2007 Share Posted May 17, 2007 <?php echo "<pre>"; print_r($list); echo "</pre>"; ?> Run that and let us know the outcome Link to comment https://forums.phpfreaks.com/topic/51894-solved-foreach-help/#findComment-255828 Share on other sites More sharing options...
sinkhead Posted May 17, 2007 Author Share Posted May 17, 2007 It was already in my code. Heh, I forgot to include it... Array ( [0] => Array ( [filename] => shinji.gif [stored_filename] => shinji.gif [size] => 271718 [compressed_size] => 271651 [mtime] => 1176744594 [comment] => [folder] => [index] => 0 [status] => ok ) [1] => Array ( [filename] => aj421.png [stored_filename] => aj421.png [size] => 96723 [compressed_size] => 94987 [mtime] => 1176626930 [comment] => [folder] => [index] => 1 [status] => ok ) [2] => Array ( [filename] => sinkhead.png [stored_filename] => sinkhead.png [size] => 268004 [compressed_size] => 268089 [mtime] => 1176315076 [comment] => [folder] => [index] => 2 [status] => ok ) ) - Sam Link to comment https://forums.phpfreaks.com/topic/51894-solved-foreach-help/#findComment-255829 Share on other sites More sharing options...
Lumio Posted May 17, 2007 Share Posted May 17, 2007 Ah! <?php foreach ($list as $i => $s) { echo 'Index: '.$i.'; '.$s['filename'].'<br />'; } ?> What you meant is to use for (without each): <?php for ($x=0; $x<count($list); $x++) { echo 'Index: '.$x.'; '.$list[$x]['filename'].'<br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/51894-solved-foreach-help/#findComment-255836 Share on other sites More sharing options...
sinkhead Posted May 17, 2007 Author Share Posted May 17, 2007 It works, thanks a lot! - Sam Link to comment https://forums.phpfreaks.com/topic/51894-solved-foreach-help/#findComment-255845 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.