首页 > 解决方案 > php 没有捕捉到下一个数组元素

问题描述

我有这个数组:

array (
  'cfop' => '3',
  'cst' => NULL,
  'cstPis' => '2',
  'cstCofins' => '1',
)  

而这段代码:

while ($t = current($taxes))
{
    $getkey = key($taxes);
    Log::debug($getkey);

    //$this->saveTaxes($getkey, $t, $add);

    $t = next($taxes);
}

ps:$taxes是数组

由于某种原因,该next函数没有传递空值,因此日志仅打印 cfop。

如果我像这样更改顺序

array (
  'cfop' => '3',
  'cst' => '2',
  'cstPis' => NULL,
  'cstCofins' => '1',
) 

将打印日志cfopcst但在 NULL 后未达到下一个值

标签: phplaravel

解决方案


null是一个假值,所以当你这样做时$t = current($taxes),它的计算结果是假的,所以这就是它停止执行的原因。

如果您想遍历每个值,我建议改为使用 foreach 循环。

foreach ($taxes as $key => $value) {
  Log::debug($key);
  // $this->saveTaxes($key, $value, $add);
}

推荐阅读