2013-04-01 132 views
0

我用PHP RecursiveDirectoryItorator玩耍,但我发现,由于某种原因,我的一些变量超出范围时,他们应该在范围......除非我真的失去了一些东西。我试图访问的两个变量是$ master_array和$ current,如果我在我的else语句中echo或print_r它们都没有任何值。这里是我的代码:为什么我没有范围来访问recursiveDirectoryItorator中的值?

$master_array = array(); 

$startpath = "some file path"; // i'm using a real file path in my code. 
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($startpath), RecursiveIteratorIterator::CHILD_FIRST); 

foreach($ritit as $fileinfo) { 
    echo "<pre>"; 
    $current = ''; 
    if(!$fileinfo->isFile()) { 

     if(strip_replace($fileinfo->getFilename()) == strip_replace('some - title')) { 
      $master_array[$fileinfo->getFilename()] = $fileinfo->getPathname(); 
      $current = $fileinfo->getFilename(); 
     } 

    } else { 

     if(!empty($current) && in_array($current, split_file_path_into_array($fileinfo->getPathname()))) { 
      echo $fileinfo->getFilename()."\n"; 
      echo $fileinfo->getPathname()."\n"; 
     } 
     echo $current; // i don't have access to this 
     print_r($master_array); // and i don't have access to this. 

     } 
    } 

任何帮助,这将不胜感激。我希望我只是忽略了一些简单的东西,但我一直在查看这些代码,并在相当一段时间内搞砸了它,似乎无法使其运行起来。

回答

1

$current应该是数组不串

$current = array(); 

而且$current将永远是空的,因为你在呼唤它的内部和其他内部if's if分配值。

+0

我试过了...它什么都不做。的事实,这是行不通的一个例子是,我甚至想打印出$ master_array ...如果我醚称之为if语句或else语句外,其打印出的值。所以我很困惑。虽然我很欣赏你的快速回复。 –

+0

@ AlexW.H.B。可能是你的条件从来没有见过,结果只有其他部分。尝试正确的调试。 –

+0

我与你的说法,“因为你在呼唤它里面否则,如果如果公司内部的分配值$当前将永远是空的。”这是有道理的......但我现在没有得到的是为什么$ master_array没有任何价值,当它的范围比foreach循环更高时。我会尝试调试它......好的建议。 –

相关问题