在二维数组中使用array_count_values



我有一个数组,旁边是补丁号和季节,我想计算一个季节有多少补丁,所以我想计算有多少$season[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'];

这里还有一个var_dump,显示了我的数组的样子https://i.stack.imgur.com/BPOp2.png

如果我没看错你的问题,你实际上是想计算内部数组而不遍历外部数组吗?如果是这种情况,请查看array_column()

例如,假设我有如下数组

$myArr = [
    ['item' => 'value1'],
    ['item' => 'value2']
]

像这样调用数组列

array_column($myArr, 'item'); // returns an array of just the values that you can iterate through.

看到

如果这不是你想要的答案,请忽略。

相关内容

  • 没有找到相关文章

最新更新