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

时间:2018-05-16 09:58:39

标签: php arrays codeigniter for-loop

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

$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 个答案:

答案 0 :(得分:3)

您可以使用array_reduceforeach

$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
        )

)

答案 1 :(得分:2)

您需要使用第二次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); 来区分记录以及正确添加

!empty()

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

注意: - 而不是is_array() && count()使用$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); 来检查它是否是一个数组并且在其中有一些值迭代传统

IF Opendialog1.Execute then
   BEGIN
      Listbox1.Items.LoadfromFile(OpenDialog1.FileName);
   END;