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