gpt4 book ai didi

php - 计算第一个数组中的键总数

转载 作者:行者123 更新时间:2023-12-02 05:03:15 24 4
gpt4 key购买 nike

Array
(
[18] => Array
(
[0] => 137585189
[1] => 138053588
)

[19] => Array
(
[0] => 137626141
[1] => 137672213
[2] => 137718802
)
)

Array
(
[18] => Array
(
[0] => 137585189
[1] => 138053588
)

[19] => Array
(
[0] => 137626141
[1] => 137672213
[2] => 137718802
[3] => 137732801
)
)

结果来自:

foreach($value as $val){
echo '<pre>';
print_r($value);
echo '</pre>';
}

如何求和每个数组的键总数?

数组 = 5

数组 = 6

最佳答案

你可以做递归 count ,通过提供选项 COUNT_RECURSIVE。用简单的计数减去递归计数就可以得到你想要的

<?php
$food = array('fruits' => array('orange', 'banana', 'apple'),
'veggie' => array('carrot', 'collard', 'pea'));

// recursive count
echo count($food, COUNT_RECURSIVE); // output 8

// normal count
echo count($food); // output 2

// to count second level entries
echo (count($food,COUNT_RECURSIVE)-count($food,0)); //output 6

?>

关于php - 计算第一个数组中的键总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16683078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com