2016-04-04 68 views
-1

我建造了遍歷一個特定的文件夾,並得到一個數組的foreach:腓的foreach in_array保持返回false

foreach ($watchFolder as $key => $value) { 
    echo '<pre>'; 
    print_r($value); 
    echo '</pre>'; 
    if((in_array('xml', $watchFolder))) { 
    echo "true"; 
    } else { 
    echo "false"; 
    } 

    foreach ($value as $key => $valueSub) { 
    $currentWatchPath2 = $currentWatchPath . '\\' . $key; 
    foreach ($valueSub as $content) { 
     $currentWatchPath3 = $currentWatchPath2 . '\\' . $content; 
    } 
    } 
} 

如果我打印出來的$watchfolder我得到這個:

Array (
    [test] => Array([videos] => Array() 
        [xml] => Array()) 

    [test2] => Array([json] => Array([0] => test.json) 
        [videos] => Array()) 
) 

如果我試試這個:

它返回null,而它應該返回真的還是?

我現在很困惑,所以我很感激每一個提示和建議。

+1

我想'in_array'只會得到不是鍵的值。 – aldrin27

+0

'in_array'只檢查值,而不檢查關鍵字。 'array_key_exists()'可能會有所幫助。 –

+0

要檢查密鑰,你可以這樣檢查:'isset($ value ['xml'])' –

回答

0

xml$value陣列的關鍵,所以我覺得你應該檢查是這樣的:

foreach ($watchFolder as $key => $value) { 
     echo '<pre>'; 
     print_r($value); 
     echo '</pre>'; 
     if(isset($value['xml'])) { // <--- here is the changed. 
      echo "true"; 
     } else { 
      echo "false"; 
     } 

     foreach ($value as $key => $valueSub) { 

      $currentWatchPath2 = $currentWatchPath . '\\' . $key; 

      foreach ($valueSub as $content) { 

       $currentWatchPath3 = $currentWatchPath2 . '\\' . $content; 

      } 

     } 

    } 
+0

我認爲這個工作,我會測試,並檢查答案,如果它繼續工作 –

+0

即使該值是''''或空白,isset()將返回true。 – aldrin27

+0

@ aldrin27是的。確實如此。如果你想在空或未設置時返回false,可以使用:'!empty($ value ['xml'])' –

0

在in_array( 'XML',$ watchFolder)的更換,就可以使用in_array( 'XML', $ value)

foreach ($watchFolder as $key => $value) { 
    echo '<pre>'; 
    print_r($value); 
    echo '</pre>'; 
    if((in_array('xml', $value))) { 
     echo "true"; 
    } else { 
     echo "false"; 
    } 

    foreach ($value as $key => $valueSub) { 

     $currentWatchPath2 = $currentWatchPath . '\\' . $key; 

     foreach ($valueSub as $content) { 

      $currentWatchPath3 = $currentWatchPath2 . '\\' . $content; 

     } 

    } 

}