1

I am trying to count the array value in the multidimensional array for the following Array value.

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

I am expecting the following output.

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

I have tried the following method using the for loop but I cannot able to achieve the above output like above.

$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;
                    }
                }
            }
        }
    }
}

Using this method I am getting the following output but value are not counting as expected.

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

I am open to suggestions also I have tried a lot of methods but still I cannot achieve it. I am stuck here for long time

Thanks in advance.

6
  • share live demo please, or share data sample in php code Commented May 16, 2018 at 10:01
  • Not sure how you count them. And why should not [0] be counted? In [1] i count 2 col1 and 2 col1, you say there is three of each? Commented May 16, 2018 at 10:02
  • @C2486 you mean a var_export? Commented May 16, 2018 at 10:04
  • php code for $allMultiGridRes , it help to run at our side Commented May 16, 2018 at 10:05
  • 1
    @Prashanth : Bhai I was talking about this array formed in php that Alive to Die given here : eval.in/1004977 Commented May 16, 2018 at 10:25

2 Answers 2

3

You can use array_reduce and 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 );

This will result to:

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

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

)
Sign up to request clarification or add additional context in comments.

5 Comments

Great Yes it works @eddie but the key index count should start with [1] ,[2].
@Prashanth Yes, That will follow the key on your array.
I used 0 and 1 on mine, so that is why you are getting that
Hi @Eddie How can I get the same kind output for the inputs give on the following link. Demo
2

You need to use second foreach() iteration key to distinguishing records as well as for proper addition

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

Output:-https://eval.in/1004977

Note:- Instead of !empty() use is_array() && count() to check that it's an array and have some value in it to iterate 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.