Php 如何获取数组中特定值的数量?
我有一个多维数组。我想获得所有数组中特定值的计数。主数组中的这个数组取决于我添加了多少注释 这是我的阵列:Php 如何获取数组中特定值的数量?,php,arrays,count,Php,Arrays,Count,我有一个多维数组。我想获得所有数组中特定值的计数。主数组中的这个数组取决于我添加了多少注释 这是我的阵列: array(3) { [0]=> array(11) { ["comment_id"]=> string(1) "1" ["user_Id"]=> string(1) "2" ["comment"]=> string(18) "Commented! edited!" ["comment_datetime"]=>
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)。只需使用foreach
$count=0;
foreach($yourarray as $a){
if($a['delete']=='1')
$count++;
}
就像上面的评论所说的,一个简单的foreach和一些数学运算。您可以使用这样的函数(需要PHP>5.5.0):
只需按如下方式应用foreach循环:
$count = 0;
foreach($yourarray as $counter){
if($counter['delete'] == "0"){
$count++;
}
}
echo $count;
你的尝试在哪里<代码>计数+数组过滤器或良好的ol'foreach就足够了
echo array_count_values(array_column($array_string, 'delete'))[0];
$count = 0;
foreach($yourarray as $counter){
if($counter['delete'] == "0"){
$count++;
}
}
echo $count;