denoteone Posted April 28, 2010 Share Posted April 28, 2010 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 More sharing options...
cags Posted April 28, 2010 Share Posted April 28, 2010 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++; } Link to comment https://forums.phpfreaks.com/topic/200081-assign-array-values-with-a-loop/#findComment-1050149 Share on other sites More sharing options...
denoteone Posted April 29, 2010 Author Share Posted April 29, 2010 now that I want to echo the first value of name in $images[]; but the following code is giving me this error. Notice: Undefined index: name echo $images_new['name'][0]; Link to comment https://forums.phpfreaks.com/topic/200081-assign-array-values-with-a-loop/#findComment-1050479 Share on other sites More sharing options...
denoteone Posted April 29, 2010 Author Share Posted April 29, 2010 had it backwards echo $images_new[0]["name"]; Link to comment https://forums.phpfreaks.com/topic/200081-assign-array-values-with-a-loop/#findComment-1050482 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.