PHP注意:获取所有目录时未定义的偏移量:1

PHP注意:获取所有目录时未定义的偏移量:1,php,while-loop,directory,notice,array-filter,Php,While Loop,Directory,Notice,Array Filter,因此,当我使用下拉菜单选择人们想要选择的样式时,我遇到了以下错误: 注意:未定义的偏移量:1英寸 守则: <?php $dirs=array_filter(glob('../styles/*'), 'is_dir'); $count=count($dirs); $i = $count; while($i>0){ echo substr($dirs[$i], 10); $i=$i-1; } ?> 我希望有人知道如何修复错误, 非常感谢 这是因为该函数从数组中

因此,当我使用下拉菜单选择人们想要选择的样式时,我遇到了以下错误:

注意:未定义的偏移量:1英寸

守则:

<?php
$dirs=array_filter(glob('../styles/*'), 'is_dir');
$count=count($dirs);
$i = $count;
while($i>0){
    echo substr($dirs[$i], 10);
    $i=$i-1;
}
?>

我希望有人知道如何修复错误, 非常感谢

这是因为该函数从数组中删除了不是目录的项。
但是键将保持不变

您可以使用
GLOB_ONLYDIR
标志代替


<?php
  $dirs   = glob( '../styles/*', GLOB_ONLYDIR );
  $count  = count( $dirs );
  $i      = ( $count - 1 ); // note: you must substract 1 from the total

  while( $i >= 0 ) {
    echo substr( $dirs[$i], 10 ); // i assumes that you want the first 10 chars, if yes use substr( $dirs[$i], 0, 10 )
    $i--;
  }

  /** With FOREACH LOOP **/
  $dirs = glob( '../styles/*', GLOB_ONLYDIR );
  $dirs = array_reverse( $dirs );

  foreach( $dirs as $dir ) {
    echo substr( $dir, 10 );
  }
?>