gpt4 book ai didi

PHP 计数数组列

转载 作者:行者123 更新时间:2023-12-03 23:06:57 31 4
gpt4 key购买 nike

我有这个 PHP 数组:

$food = array('fruits' => array('orange', 'banana', 'apple'),
'veggie' => array('carrot', 'collard', NULL));

如果我使用这个功能:
$rows = count($food);

当然结果是 2,但是如何获取数组列的数量呢?所以,我期待 3 作为水果和蔬菜列的值。即使 veggie 有 NULL。

最佳答案

你可以试试这个:array_mapcount .

<?php 
$food = array('fruits' => array('orange', 'banana', 'apple'),
'veggie' => array('carrot', 'collard', NULL));
$typeTotals = array_map("count", $food);
echo "<pre>";
print_r($typeTotals);

输出:

Array ( [fruits] => 3 [veggie] => 3 )

关于PHP 计数数组列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34984932/

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