为什么0的值不在foreach循环中传递?

Con*_*nor 1 php arrays foreach

在将代码添加到Production之前尝试测试我的代码,但为什么foreach循环只传递3个值$strokes而不是所有4个$_POST数组值.其中一个是0.

代码: https ://ideone.com/qBO4rx

$_POST = array("h1" => 1, "h2" => 2, "h3" => 3, "h4" => 0);
$strokes = array();

$strokes_keys = array('h1', 'h2', 'h3', 'h4');

    foreach ($strokes_keys as $stroke) {
      if ($_POST[$stroke]) {
          array_push($strokes, $_POST[$stroke]);
        }
    }

    $counts = count($strokes);

    var_dump($strokes);
    var_dump($counts);
Run Code Online (Sandbox Code Playgroud)

结果:

Success time: 0.04 memory: 52480 signal:0
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
int(3)
Run Code Online (Sandbox Code Playgroud)

Ahm*_*mad 5

array_push($strokes, $_POST[$stroke]);不执行的最后一个项目,因为零等于false.也许你应该尝试:

if (isset($_POST[$stroke])) {
     array_push($strokes, $_POST[$stroke]);
}
Run Code Online (Sandbox Code Playgroud)

代替.