代码之家  ›  专栏  ›  技术社区  ›  Prashanth

使用php动态多维数组值计数

  •  1
  • Prashanth  · 技术社区  · 6 年前

    我正在尝试为以下数组值计算多维数组中的数组值。

    $allMultiGridRes = Array
    (
        [0] => Array
            (
                [1] => Array
                    (
                        [1] => grid multi col1
                        [2] => grid multi col2
                    )
                [2] => Array
                    (
                        [1] => grid multi col1
                    )
            )
        [1] => Array
            (
                [1] => Array
                    (
                        [1] => grid multi col1
                        [2] => grid multi col2
                    )
                [2] => Array
                    (
                        [1] => grid multi col1
                        [2] => grid multi col2
                    )
            )
        [2] => Array
            (
                [1] => Array
                    (
                        [1] => grid multi col1
                        [2] => grid multi col2
                    )
                [2] => Array
                    (
                        [1] => grid multi col1
                        [2] => grid multi col2
                    )
            )
    )
    

    我期待以下输出。

    Array
    (
        [1] => Array
            (
                [grid multi col1] => 3
                [grid multi col2] => 3
            )
        [2] => Array
            (
                [grid multi col1] => 3
                [grid multi col2] => 2
            )
    )
    

    我使用 for loop 但我无法实现上述输出。

    $allMultiGridRes =array();
    $paramCheck =array();
    foreach ($allMultiGridRes as $gMKey => $GMvalue){
        if(!empty($GMvalue)){
            foreach ($GMvalue as $gMKey2 => $gMValue2){
                if(!empty($gMValue2)){
                    $allGridCount[$gMKey2] = array_count_values($gMValue2);
                    foreach ($gMValue2 as $gMKey3 => $gMValue3){
                        $paramCheck = !empty($allGridCount[$gMKey2])?$allGridCount[$gMKey2]:array();
                        $GMindex1 = $gMValue3;
                        $allGridCount[$gMKey2][$GMindex1] = array_key_exists($GMindex1,$paramCheck) ? $allGridCount[$gMKey2][$GMindex1]++ : 1;
                        }
                    }
                }
            }
        }
    }
    

    使用此方法,我得到以下输出,但值没有按预期计数。

    Array
    (
        [1] => Array
            (
                [grid multi col1] => 1
                [grid multi col2] => 1
            )
        [2] => Array
            (
                [grid multi col1] => 1
                [grid multi col2] => 1
            )
    )
    

    我愿意接受建议,我也尝试了很多方法,但仍然无法实现。我被困在这里很久了

    提前谢谢。

    2 回复  |  直到 6 年前
        1
  •  3
  •   Eddie    6 年前

    你可以用 array_reduce foreach

    $allMultiGridRes = ;//Your array
    
    $result = array_reduce($allMultiGridRes, function($c, $v){
        foreach ( $v as $key => $value ) {
            if ( !isset( $c[$key] ) ) $c[$key] = array();
    
            foreach( $value as $o ) {
                if ( !isset( $c[$key][$o] ) ) $c[$key][$o] = 0;
                $c[$key][$o]++;
            }
        }
        return $c;
    },array());
    
    print_r( $result );
    

    这将导致:

    Array
    (
        [1] => Array
            (
                [grid multi col1] => 3
                [grid multi col2] => 3
            )
    
        [2] => Array
            (
                [grid multi col1] => 3
                [grid multi col2] => 2
            )
    
    )
    
        2
  •  2
  •   Death-is-the-real-truth    6 年前

    您需要使用second foreach() 迭代 key 区分记录以及适当添加

    $allGridCount = [];
    foreach ($allMultigridRes as $gMKey => $GMvalue){
        if(!empty($GMvalue)){
            foreach ($GMvalue as $gMKey2 => $gMValue2){
                if(!empty($gMValue2)){
                    foreach ($gMValue2 as $gMKey3 => $gMValue3){
                        $allGridCount[$gMKey2][$gMKey3][$gMValue3] = (isset($allGridCount[$gMKey2][$gMKey3][$gMValue3])) ? $allGridCount[$gMKey2][$gMKey3][$gMValue3]+1 : 1;
                    }
                }
            }
        }
    }
    echo "<pre/>";print_r($allGridCount);
    

    输出:- https://eval.in/1004977

    注:- 而不是 !empty() 使用 is_array() && count() 检查它是否是一个数组,并在其中包含一些值以迭代furhter

    $allGridCount = [];
    foreach ($allMultigridRes as $GMvalue){
        if(is_array($GMvalue) && count($GMvalue) >0){
            foreach ($GMvalue as $gMKey2 => $gMValue2){
                if(is_array($gMValue2) && count($gMValue2) >0){
                    foreach ($gMValue2 as $gMKey3 => $gMValue3){
                        $allGridCount[$gMKey2][$gMKey3][$gMValue3] = (isset($allGridCount[$gMKey2][$gMKey3][$gMValue3])) ? $allGridCount[$gMKey2][$gMKey3][$gMValue3]+1 : 1;
                    }
                }
            }
        }
    }
    echo "<pre/>";print_r($allGridCount);