I've got an array and I need count the keys with a specific value, which is proving to be a nightmare.
我有一个数组,我需要用一个特定值来计算键值,这被证明是一场噩梦。
Array([0] => Array
(
[0] => 1
[ruleid] => 1
[1] => Test Outbound Life 1
[rule_name] => Test Outbound Life 1
[2] => Life Insurance
[product_type] => Life Insurance
[3] => 1
[status] => 1
[4] => 1000
[priority] => 1000
[5] => 100
[quantity] => 100
[6] => 1-2-3-4-5-6-7-
[dayofweek] => 1-2-3-4-5-6-7-
[7] => 2
[income] => 2
[8] => external/arc.php
[integrationfile] => external/arc.php
[9] => 1
[partnerid] => 1
)
[1] => Array
(
[0] => 2
[ruleid] => 2
[1] => Test Outbound Life 2
[rule_name] => Test Outbound Life 2
[2] => Life Insurance
[product_type] => Life Insurance
[3] => 1
[status] => 1
[4] => 800
[priority] => 800
[5] => 100
[quantity] => 100
[6] => 1-2-3-4-5-6-7-
[dayofweek] => 1-2-3-4-5-6-7-
[7] => 2
[income] => 2
[8] => test.php
[integrationfile] => test.php
[9] => 1
[partnerid] => 1
) )
The array will be generated dynamically so the same array will appear in the array.
该数组将被动态生成,因此将在数组中显示相同的数组。
I want to count how many times the same ruleid appears is it will look like this:
我要数一下这个ruleid出现的次数是这样的:
Array{
[1] => 1
[2] => 1
}
Update: I need to count how many times ruleid = 2 or how many times ruleid = 1
更新:我需要计算ruleid = 2或ruleid = 1的次数。
2 个解决方案
#1
0
So you want to count the number of time each ruleid
appears inside an array. Let's call this array $count
. This is how I'd do it.
所以你要计算每个ruleid在数组中出现的时间。我们称这个数组为$count。我就是这么做的。
$count = array();
foreach($arrays as $array) { // $arrays is your big ass array containing arrays
// increment the value with the key corresponding to ruleid (improved by JustOnUnderMillions)
$count[$array['ruleid']] = isset($count[$array['ruleid']]) ? ($count[$array['ruleid']] + 1) : 1;
}
print_r(count); // should give you what you're looking for
#2
0
I have worked it out by looping and counting how many times a ruleid appears in an array.
我通过循环计算并计算一个ruleid在数组中出现的次数。
foreach ($count as $key => $value) {
$c = 0;
//check that outbound has passed all rules
foreach ($out as $k => $v) {
if ($v['ruleid']==$key) {
$c +=1;
}
}
if ($c==$value) {
//add valid outbound to array
foreach ($out as $k => $v) {
if ($v['ruleid']==$key) {
$valid[$key] = $v;
}
}
}
}
The loop checks the ruleid has passed all rules I had already counted in the $count variable.
循环检查ruleid已经通过了我在$count变量中已经计算过的所有规则。
#1
0
So you want to count the number of time each ruleid
appears inside an array. Let's call this array $count
. This is how I'd do it.
所以你要计算每个ruleid在数组中出现的时间。我们称这个数组为$count。我就是这么做的。
$count = array();
foreach($arrays as $array) { // $arrays is your big ass array containing arrays
// increment the value with the key corresponding to ruleid (improved by JustOnUnderMillions)
$count[$array['ruleid']] = isset($count[$array['ruleid']]) ? ($count[$array['ruleid']] + 1) : 1;
}
print_r(count); // should give you what you're looking for
#2
0
I have worked it out by looping and counting how many times a ruleid appears in an array.
我通过循环计算并计算一个ruleid在数组中出现的次数。
foreach ($count as $key => $value) {
$c = 0;
//check that outbound has passed all rules
foreach ($out as $k => $v) {
if ($v['ruleid']==$key) {
$c +=1;
}
}
if ($c==$value) {
//add valid outbound to array
foreach ($out as $k => $v) {
if ($v['ruleid']==$key) {
$valid[$key] = $v;
}
}
}
}
The loop checks the ruleid has passed all rules I had already counted in the $count variable.
循环检查ruleid已经通过了我在$count变量中已经计算过的所有规则。