我有一个数组,我想获得具体值的计数。
结构如下:
Array (
[0] => Array (
[coupon_id] => 350
[coupon_title] => This is the title of coupons
[coupon_code] => ABCD
[coupon_type] => coupon
)
[1] => Array (
[coupon_id] => 350
[coupon_title] => This is the title of coupons
[coupon_code] => ABCD
[coupon_type] => no_coupon
)
)
现在我想得到coupon
和no_coupon
我试过array_value_count
,但我知道这只能计算STRING和VALUES。
答案 0 :(得分:2)
<?php
# Get the data from where you want to get the count
$array = array(
array('coupon_type' => 'coupon'),
array('coupon_type' => 'no_coupon')
);
# Count the variables
$count = array_count_values(array_column($array, 'coupon_type')));
print_r($count);
// Will show
// Array ( [coupon] => 1 [no_coupon] => 1 )
?>
答案 1 :(得分:0)
您可以使用foreach循环来实现此目的。
$count_coupon=0;
$count_no_coupon=0;
foreach ( $array as $arr )
{
if( $arr['coupon_type'] == 'coupon' )
{
$count_coupon++;
}
else if( $arr['coupon_type'] == 'no_coupon' )
{
$count_no_coupon++;
}