0

我需要代码来重新启动每个 $item 的计数

我的代码是:

 $count = 0;
 $folders = @scandir('Users/');  
 foreach($folders as $item){
   if (preg_match("/.*\.php$/",$item)) continue;
   echo $item;
   if (is_dir("Users/$item")){
     $target_folders = @scandir("Users/$item/uploaded/");
     foreach($target_folders as $target_item){
       if ((!preg_match("/^[.]/",$item)) ||(!is_dir("Users/$item/uploaded/$target_item"))) $count++;
     }
   }
 }

如果可以做到这一点,我们将不胜感激。这是我项目的最后一点。

4

2 回答 2

1

在第一个 foreach 循环内将 $count 变量设置为零(如 u_mulder 建议的那样)

$count = 0;
$folders = @scandir('Users/');

foreach($folders as $item){

      $count = 0;
      if (preg_match("/.*\.php$/",$item)) 
           continue;

      echo $item;

      if (is_dir("Users/$item")){

           $target_folders = @scandir("Users/$item/uploaded/");
           foreach($target_folders as $target_item){

                if ((!preg_match("/^[.]/",$item)) || (!is_dir("Users/$item/uploaded/$target_item")))
                     $count++;

           }
      }
 }
于 2013-09-17T21:07:49.450 回答
0

尝试这个:

$count_result=array();
$path = "Users";
$folders = @scandir("$path");
if ($folders) {
    foreach ($folders as $item) {
        $count = 0;
        if ((substr($item, 0, 1) == '.') || (preg_match("/\.php$/", $item)))
            continue;

        if (is_dir($path . "/" . $item)) {
            echo "$item<br>";


            $target_folders = @scandir($path . "/" . $item . "/uploaded/");
            if ($target_folders) {
                foreach ($target_folders as $target_item) {
                    if ((substr($target_item, 0, 1) == '.') || is_dir("$path/$item/uploaded/$target_item"))
                        continue;
                    $count++;

                }
            }
            $count_result[$item]=$count;

        }
    }
}

var_dump($count_result);
于 2013-09-17T21:29:59.163 回答