以下是数组,如果product
name
是Powerwall,我想要数一数
Array
(
[0] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[1] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[2] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[3] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[4] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[5] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[6] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[7] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Powerwall
[category] => product
)
)
[8] => Array
(
[user] => 58cb6fb8bf3c3600043ec1f2
[company] => tesla
[product] => Array
(
[name] => Model S
[category] => product
)
)
)
需要输出8 for Powerwall and 1 for Model S
答案 0 :(得分:1)
每次找到 Powerwall 时,您需要遍历数组并递增计数器$count
。
$count = 0;
foreach ($array as $arr) {
if ($arr['product']['name'] == 'Powerwall') {
$count++;
}
}
echo "Powerwall count = " . $count;
答案 1 :(得分:0)
只需使用array_column
和array_count_values
来计算号码
<?php
$dataarray=Array
(
0 => Array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => Array
(
"name" => "Powerwall",
"category" => "product",
)
),
1 => Array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
2 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
3 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
4 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
5 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
6 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
7 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Powerwall",
"category" => "product",
)
),
8 => array
(
"user" => "58cb6fb8bf3c3600043ec1f2",
"company" => "tesla",
"product" => array
(
"name" => "Model S",
"category" => "product",
)
),
);
$result= array_count_values(array_column(array_column($dataarray,"product"),"name"));
echo "Count: ".$result["Powerwall"];