Jump to content

Assign array values with a loop


denoteone

Recommended Posts

I am trying to loop through a directory and assign the file names and file sizes to an array like so.

 


$i = 0;
while (($file = readdir($dh)) !== false) {
if($file != '.' && $file != '..' && !is_dir($dir.$file)){
$images[file[$i]  => $file, file_size[$i]  => filesize($file) ];

}
$i++;
}

but this part of the code is breaking my page.

Link to comment
https://forums.phpfreaks.com/topic/200081-assign-array-values-with-a-loop/
Share on other sites

It's not entirely clear what you wish the output to be. But I'm guessing you want something like this...

 

$i = 0;
$images = array();
while (($file = readdir($dh)) !== false) {
   if($file != '.' && $file != '..' && !is_dir($dir.$file)){
      $images[] = array('name' => $file, 'size'  => filesize($file));
   }
   $i++;
}

 

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.