php - 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',
)
将打印日志cfop
,cst
但在 NULL 后未达到下一个值
解决方案
null
是一个假值,所以当你这样做时$t = current($taxes)
,它的计算结果是假的,所以这就是它停止执行的原因。
如果您想遍历每个值,我建议改为使用 foreach 循环。
foreach ($taxes as $key => $value) {
Log::debug($key);
// $this->saveTaxes($key, $value, $add);
}
推荐阅读
- mysql - 从 mysql 中的 csv 文件加载数据时无法转换日期格式
- r - Shiny,使用自写函数创建和渲染表格
- python - Transformer 中 Optional[Tensor] 是什么意思?
- python - 使用 [start:stop:step] 在 python 中反转一段字符串
- typescript - 如何在使用 botbuilder 的机器人中将实体标识为 Microsoft Teams 上下文中的标签?
- android - 如何将字符串转换为小时、分钟、秒的时间作为颤动中的整数?
- python - 请求库在 Twitter / Python 上不起作用
- pytorch - AttributeError:“DataModuleClass”对象没有属性“training_dataset”
- mysql - MYSQL:用团队中的顺序更新表
- asp.net-core - 如何解决缺少对 Ifragistics UI 的引用 - CS1061 编译器错误