So I have array with patch number and seasons next to it, and I'm trying to count how many patches are in a season so I want to count how many $season[x][1] == 1 or 2 etc.
所以我有一个数组,上面有补丁数和季节,我想计算一个季度有多少个补丁,所以我想计算一个季度(x)[1] == 1或2等等。
$patches_get = $conn->prepare("SELECT Patch_No FROM info ORDER BY Created DESC");
$patches_get->execute();
$patchesresult = $patches_get->get_result();
while($data1 = $patchesresult->fetch_assoc()){
$patches[]=$data1["Patch_No"];
}
function getseasons($patches){
$seasons = array();
foreach($patches as $patch){
if(substr($patch,0,1)!=1){
$seasons[] = array($patch, substr($patch,0,1));
}
//Checking first number if 1 it is season 1 or 2 or 3
elseif(substr($patch,0,1)==1){
if(substr($patch, 6,3)>151&&substr($patch, 6,3)<155){
$seasons[] = array($patch, 3);
}
elseif(substr($patch, 6,3)>125&&substr($patch, 6,3)<151){
$seasons[] = array($patch, 2);
}
elseif(substr($patch, 6,3)>32&&substr($patch, 6,3)<126){
$seasons[] = array($patch, 1);
}
}
}
return $seasons;
}
$seasons = getseasons($patches);
var_dump($seasons);
$fr_c=array_count_values($seasons);
echo $fr_c['1'];
Here is also a var_dump of how my array would look like http://i.imgur.com/lV1APvV.png
这里还有一个var_dump,它显示了我的数组看起来像http://i.imgur.com/lV1APvV.png
1 个解决方案
#1
0
If I"m reading your question right, you essentially want to count the inner array without iterating through the outer? If that's the case, look into array_column()
如果我读对了你的问题,你基本上想要计算内部数组而不是遍历外部数组?如果是这样,请查看array_column()
For example, let's say I have the following array
例如,假设我有以下数组
$myArr = [
['item' => 'value1'],
['item' => 'value2']
]
Calling array column like so
像这样调用数组列
array_column($myArr, 'item'); // returns an array of just the values that you can iterate through.
See here
在这里看到的
If this isn't the answer you're looking for, please disregard.
如果这不是你想要的答案,请忽略它。
#1
0
If I"m reading your question right, you essentially want to count the inner array without iterating through the outer? If that's the case, look into array_column()
如果我读对了你的问题,你基本上想要计算内部数组而不是遍历外部数组?如果是这样,请查看array_column()
For example, let's say I have the following array
例如,假设我有以下数组
$myArr = [
['item' => 'value1'],
['item' => 'value2']
]
Calling array column like so
像这样调用数组列
array_column($myArr, 'item'); // returns an array of just the values that you can iterate through.
See here
在这里看到的
If this isn't the answer you're looking for, please disregard.
如果这不是你想要的答案,请忽略它。