Jump to content

[SOLVED] Foreach help


sinkhead

Recommended Posts

$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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.