2013-04-01 131 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循環更高時。我會嘗試調試它......好的建議。 –

相關問題