PHP count() returns 非空数组上的 NULL
PHP count() returns NULL on non-empty array
我将一大堆数据填充到一个数组中(通过从内存缓存中提取数据或查询数据库)。我想计算这个数组中有多少行以确保它大于 0。当我使用 count() 时,它 returns 什么也没有,即使数组充满了数据。
为了排除故障,我尝试了以下代码:
$item_count=count($item_data);
print_r($item_data);
die($item_count);
这打印出一个巨大的数组,我可以看到它有 146 个元素。但是 $item_count 是 NULL。知道什么可能是错的吗?谢谢!
根据评论中的要求,这是我尝试计算的数组的示例:
Array
(
[0] => Array
(
[calories] => 190
[healthy_option] => 0
[name] => Chicken McNuggets
[url_name] => chicken-mcnuggets
[category_name] => Chicken Nuggets and Strips
[category_url_name] => chicken-nuggets-strips
[category_id] => 85
)
[1] => Array
(
[calories] => 380
[healthy_option] => 0
[name] => Chicken Selects Premium Breast Strips - 3 piece
[url_name] => chicken-selects-premium-breast-strips-3-piece
[category_name] => Chicken Nuggets and Strips
[category_url_name] => chicken-nuggets-strips
[category_id] => 85
)
)
die() function behaves like exit()。
这意味着 Integer 参数被视为 "status"。
来自文档:
If status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.
如果你想打印一个值,我建议你使用其他的东西,比如回声,print_r,等等
不使用die()的你是否也有同样的烦恼?
我将一大堆数据填充到一个数组中(通过从内存缓存中提取数据或查询数据库)。我想计算这个数组中有多少行以确保它大于 0。当我使用 count() 时,它 returns 什么也没有,即使数组充满了数据。
为了排除故障,我尝试了以下代码:
$item_count=count($item_data);
print_r($item_data);
die($item_count);
这打印出一个巨大的数组,我可以看到它有 146 个元素。但是 $item_count 是 NULL。知道什么可能是错的吗?谢谢!
根据评论中的要求,这是我尝试计算的数组的示例:
Array
(
[0] => Array
(
[calories] => 190
[healthy_option] => 0
[name] => Chicken McNuggets
[url_name] => chicken-mcnuggets
[category_name] => Chicken Nuggets and Strips
[category_url_name] => chicken-nuggets-strips
[category_id] => 85
)
[1] => Array
(
[calories] => 380
[healthy_option] => 0
[name] => Chicken Selects Premium Breast Strips - 3 piece
[url_name] => chicken-selects-premium-breast-strips-3-piece
[category_name] => Chicken Nuggets and Strips
[category_url_name] => chicken-nuggets-strips
[category_id] => 85
)
)
die() function behaves like exit()。 这意味着 Integer 参数被视为 "status"。
来自文档:
If status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.
如果你想打印一个值,我建议你使用其他的东西,比如回声,print_r,等等
不使用die()的你是否也有同样的烦恼?