【发布时间】:2022-11-17 12:48:00
【问题描述】:
为什么这仍然返回 3 的计数?
$arr =
[
[
'slug' => 'products-services-pricing',
'text' => 'Products/Services and Pricing',
],
[
'slug' => 'promotions-plan',
'text' => 'Promotions Plan',
],
(1 == 2) ?
[
'slug' => 'distribution-plan',
'text' => 'Distribution Plan',
] : null,
];
echo "Count = ".count($arr)."\n";
print_r($arr);
我的foreach 搞砸了。 PHP 8.0
我无法在 foreach 中进行条件检查,因为我正在使用 count。
【问题讨论】:
-
第三个元素的值为
null,没有问题,没有谜。您没有删除该元素,而是为其分配了一个值:null。在这里您有了一个想法:遍历数组并删除值为null的元素。或者简单地迭代以计数不是null元素。
标签: php