急!!!!!求大神指点 php中的count()函数如何在二维数组中使用
我创建了一个二维数组如下:顺序是品牌,颜色,大小,价钱$pens=array("p1"=>array("brand"=>"aaa","color"=>"red","siz...
我创建了一个二维数组如下:顺序是 品牌,颜色,大小,价钱
$pens=array(
"p1"=>array("brand"=>"aaa","color"=>"red","size"=>11,"price"=>20),
"p2"=>array("brand"=>"bbb","color"=>"yellow","size"=>10,"price"=>20),
"p3"=>array("brand"=>"aaa","color"=>"blue","size"=>12,"price"=>30),
"p3"=>array("brand"=>"ccc","color"=>"blue","size"=>12,"price"=>30)
"p4"......(省略)....
"p100...";
);
现在我想找到不同品牌下有多少个商品存货,怎么做?就是比如有多少个aaa,多少个bbb。
严格说应该是在总共有多少brand,brand里面有多少个aaa,多少个bbb。 展开
$pens=array(
"p1"=>array("brand"=>"aaa","color"=>"red","size"=>11,"price"=>20),
"p2"=>array("brand"=>"bbb","color"=>"yellow","size"=>10,"price"=>20),
"p3"=>array("brand"=>"aaa","color"=>"blue","size"=>12,"price"=>30),
"p3"=>array("brand"=>"ccc","color"=>"blue","size"=>12,"price"=>30)
"p4"......(省略)....
"p100...";
);
现在我想找到不同品牌下有多少个商品存货,怎么做?就是比如有多少个aaa,多少个bbb。
严格说应该是在总共有多少brand,brand里面有多少个aaa,多少个bbb。 展开
1个回答
展开全部
<?php
$pens=array(
"p1"=>array("brand"=>"aaa","color"=>"red","size"=>11,"price"=>20),
"p2"=>array("brand"=>"bbb","color"=>"yellow","size"=>10,"price"=>20),
"p3"=>array("brand"=>"aaa","color"=>"blue","size"=>12,"price"=>30),
"p4"=>array("brand"=>"ccc","color"=>"blue","size"=>12,"price"=>30)
//"p4"......(省略)....
//"p100...";
);
getBrands($pens);
function getBrands($arr){
$temp=array();
foreach($arr as $v){
$temp[]=$v['brand'];
}
$brarr=array_count_values($temp);
$brandCount=count($brarr);
echo "共".$brandCount.'个品牌';
echo "<br>";
foreach ($brarr as $k=>$v) {
echo $k.':'.$v;
echo "<br>";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询