Php foreach in_array继续返回false

时间:2016-04-04 08:41:40

标签: php arrays

我构建了一个循环遍历特定文件夹并获取数组的foreach:

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())
)

如果我试试这个:

dd(in_array('xml', $value));

它返回null,而它应该返回true或?

此刻我很困惑,所以我感谢每一个提示和建议。

2 个答案:

答案 0 :(得分: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;

            }

        }

    }

答案 1 :(得分: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;

        }

    }

}