Arrays 多维数组中匹配值的php计数
大家好,过去几个小时我一直在努力解决这个问题 我有这个数组Arrays 多维数组中匹配值的php计数,arrays,count,matching,Arrays,Count,Matching,大家好,过去几个小时我一直在努力解决这个问题 我有这个数组 Array ( [0] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php) [1] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php) [2] => Array ( [co
Array (
[0] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
[1] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
[2] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
[3] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
[4] => Array ( [countThis_id] => 5 [icon] => add.gif [url] => add.php)
[5] => Array ( [countThis_id] => 6 [icon] => add.gif [url] => add.php)
[6] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)
[7] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)
)
所以我想在这里做的是在一定条件下计算匹配的[countThis_id]
例如:
count first = if [countThis_id] == 2
count second = if [countThis_id] == 1
count the rest ignoring the matchs
这是我想要的最终结果
Array (
[0] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
[1] => Array ( [countThis_id] => 1 [icon] => add.gif [url] => add.php)
[2] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
[3] => Array ( [countThis_id] => 2 [icon] => add.gif [url] => add.php)
[4] => Array ( [countThis_id] => 5 [icon] => add.gif [url] => add.php)
[5] => Array ( [countThis_id] => 6 [icon] => add.gif [url] => add.php)
[6] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)
[7] => Array ( [countThis_id] => 7 [icon] => add.gif [url] => add.php)
[8] => Array ( [count_1] => 2 [count_2] => 2 [the_Rest] => 4)
)
有什么想法吗?谢谢你
$result = array('count_1' => 0, 'count_2'=>0, 'the_Rest'=>0);
foreach($array as $arr){
if($arr['countThis_id'] == 2){
$result['count_1']++;
}
else if($arr['countThis_id'] == 1){
$result['count_1']++;
}
else {
$result['the_rest']++;
}
}
array_push($array, $result);
var_dump($array);
对于排序,您可以使用您知道您的数组代码格式不正确吗?索引周围不应该有方括号,而且,[icon]应该是“icon”,add.gif应该是“add.gif”,[url]应该是“url”,等等。。。