小编Con*_*nor的帖子

为什么0的值不在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)

php arrays foreach

1
推荐指数
1
解决办法
34
查看次数

标签 统计

arrays ×1

foreach ×1

php ×1