在二维数组中使用array_count_values

时间:2015-08-22 21:50:59

标签: php arrays multidimensional-array

所以我的阵列旁边有补丁号码和季节,而且我试图计算一个赛季中有多少补丁,所以我想算一下多少$ 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'];

这也是我的数组看起来像http://i.imgur.com/lV1APvV.png

的var_dump

1 个答案:

答案 0 :(得分:0)

如果我正确地阅读你的问题,你基本上想要计算内部数组而不迭代外部?如果是这种情况,请查看array_column()

例如,假设我有以下数组

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

像这样调用数组列

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

请参阅here

如果这不是您正在寻找的答案,请忽略。