2015-02-24 65 views
0

我有一個多維數組。我想在所有數組中獲得特定值的計數。主數組中的這個數組取決於我添加了多少條評論。如何獲取數組中特定值的數量?

這裏是我的數組:

array(3) { 
    [0]=> 
    array(11) { 
    ["comment_id"]=> string(1) "1" 
    ["user_Id"]=> string(1) "2" 
    ["comment"]=> string(18) "Commented! edited!" 
    ["comment_datetime"]=> string(19) "2015-02-20 04:24:28" 
    ["update_at"]=> string(19) "2015-02-20 04:23:18" 
    ["user"]=> string(18) "Nishan Weerasinghe" 
    ["delete"]=> string(1) "0" 
    ["username"]=> string(6) "Nishan" 
    ["picture_id"]=> NULL 
    ["picture"]=> NULL 
    ["picture_ext"]=> NULL 
    } 

    [1]=> 
    array(11) { 
    ["comment_id"]=> string(2) "48" 
    ["user_Id"]=> string(1) "2" 
    ["comment"]=> string(4) "here" 
    ["comment_datetime"]=> string(19) "2015-02-23 12:58:00" 
    ["update_at"]=> string(0) "" 
    ["user"]=> string(18) "Nishan Weerasinghe" 
    ["delete"]=> string(1) "0" 
    ["username"]=> string(6) "Nishan" 
    ["picture_id"]=> NULL 
    ["picture"]=> NULL 
    ["picture_ext"]=> NULL 
    } 
    [2]=> 
    array(11) { 
    ["comment_id"]=> string(2) "49" 
    ["user_Id"]=> string(1) "2" 
    ["comment"]=> string(3) "dfg" 
    ["comment_datetime"]=> string(19) "2015-02-23 14:46:56" 
    ["update_at"]=> string(0) "" 
    ["user"]=> string(18) "Nishan Weerasinghe" 
    ["delete"]=> string(1) "1" 
    ["username"]=> string(6) "Nishan" 
    ["picture_id"]=> NULL 
    ["picture"]=> NULL 
    ["picture_ext"]=> NULL 
    } 
} 

我想了解有多少["delete"]=> 0(答案應該按照本恩是2)計數是有主陣列中。

+0

哪裏是你的企圖? 'count + array_filter'或者好的foreach應該就足夠了 – Ghost 2015-02-24 03:31:15

回答

2
$counter = 0; 
foreach($results as $result){ 
    if($result['delete'] == "0"){ 
     $counter++; 
    } 
} 
echo $counter; 

像評論說,一個簡單的foreach和一些數學。

0

只是使用的foreach

$count=0; 
foreach($yourarray as $a){ 

    if($a['delete']=='1') 
     $count++; 

} 
0

你可以用這樣的功能做到這一點(需要PHP> 5.5.0):

echo array_count_values(array_column($array_string, 'delete'))[0]; 
0

只是適用foreach循環如下:

$count = 0; 
foreach($yourarray as $counter){ 
    if($counter['delete'] == "0"){ 
     $count++; 
    } 
} 
echo $count; 
相關問題