使用PHP将项目数计数到多维数组中

时间:2019-04-14 19:45:32

标签: php arrays

假设我有这个数组:

Array
(
    [Europa] => Array
        (
            [country] => France
            [capital] => Paris
        ),
        (
            [country] => Spain
            [capital] => Madrid
        )
    [Asia] => Array
        (
            [country] => Russia
            [capital] => Moscow
        )
)

如何计算Asia或Europa`中包含的项目数?

谢谢。

2 个答案:

答案 0 :(得分:0)

假设

  $myArray= [
    'Europa' => [
            'country' => France
            'capital' => Paris
        ],
        [
            'country' => Spain
            'capital' => Madrid
        ],
    'Asia' => [
            'country' => Russia
            'capital' => Moscow
        ],
  ];

您可以基于索引使用count()

$numEuropa = count($myArray['Europa']);
$numAsia = count($myArray['Asia']);

答案 1 :(得分:0)

count中的array_map用作:

$cnts = array_map("count", $array);

或带有循环:

foreach($array as $key => $val)
    echo $key . " count is: " . count($value);

实时示例:3v4l