2016-07-30 12 views
0

私は配列を持っており、特定の値の数を取得したい。PHP配列から特定の値の数を取得

構造としてある:

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 
    ) 
) 

今私は私がarray_value_countで試みたが、私はこれが唯一のSTRINGと値をカウントすることができます取得していますcouponno_coupon

の数を取得したいです。

+0

'array_value_count(array_column( 'coupon_type'));それは – splash58

答えて

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) 
?> 
+0

おかげ - あなたは合計、または発生の数字をしたいですか。私は 'array_count_values'は、キーが配列に存在し、その値の合計ではない回数を返します。決して値が配列内にある回数は気にしないでください...これはうまくいくはずです。 – mohit

+0

@mohitを働いた' – ArtisticPhoenix

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++; 
     } 
関連する問題