How to specifically count arrays inside of an array? For example:
$array = [ 10, 'hello', [1, 2, 3, ['hi', 7]], [15, 67], 12 ]
Output should be 3.
CodePudding user response:
You can use a recursive function for that. Let me give you an example:
<?php
$array = [ 10, 'hello', [1, 2, 3, ['hi', 7]], [15, 67], 12 ];
$arrayCount = 0; # this variable will hold count of all arrays found
# Call a recursive function that starts with taking $array as an input
# Recursive function will call itself from within the function
countItemsInArray($array);
function countItemsInArray($subArray) {
# access $arrayCount that is declared outside this function
global $arrayCount;
# loop through the array. Skip if the item is NOT an array
# if it is an array, increase counter by 1
# then, call this same function by passing the found array
# that process will continue no matter how many arrays are nested within arrays
foreach ($subArray as $x) {
if ( ! is_array($x) ) continue;
$arrayCount ;
countItemsInArray($x);
}
}
echo "Found $arrayCount arrays\n";
Example
https://rextester.com/SOV87180
Explanation
Here's how the function would operate.
[ 10, 'hello', [1, 2, 3, ['hi', 7]], [15, 67], 12 ]
is sent tocountItemsInArray
function- This function looks at each item in the array
- 10 is reviewed. It's not an array, so the function goes to the next item
- 'hello' is reviewed. It's not an array, so the function goes to the next item
- [1, 2, 3, ['hi', 7]] is evaluated. It is an array, so arrayCount is increased to 1 and the same function is called but the input to the function now is
[1, 2, 3, ['hi', 7]
.
Remember, this same function called with the entire array is not dead. It's just waiting for this countItemsInArray([1, 2, 3, ['hi', 7]])
to be done
- 1 is evaluated. It's not an array, so the next item is evaluated
- 2 is evaluated ...
- 3 is evaluated ...
- ['hi', 7] is evaluated. It is an array. So, array count is increased to 2
countItemsInArray(['hi', 7])
is called.
Remember that countItemsInArray with the full array as the parameter AND
countItemsInArray([1, 2, 3, ['hi', 7]]) are now waiting for countItemsInArray(['hi', 7])
to be done
- hi is evaluated. That's not an array, so the function tests the next item
- 7 is evaluated. That's not an array either. So, the that function completes, giving control back to
countItemsInArray([1, 2, 3, ['hi', 7]])
countItemsInArray([1, 2, 3, ['hi', 7]])
recognizes that it has nothing more to evaluate. Control is given back to countItemsInArray($array).
- [15, 67] is evaluated. It is an array, so array count is increased to 3
- countItemsInArray([15, 67]) is called, which evaluates 15 and 16 and determines that none of them are an array, so it gives control back to countItemsInArray($array)
countItemsInArray($array) evaluates 12 and determines that's not an array either. So, it ends its work.
Then, echo echoes out that array count is 3.
CodePudding user response:
Recursively iterate your input array and restart the tally variable upon entering a new level in the array. Every time an array is encountered, add one and recurse. Pass up the tallies from the deeper levels and when the levels are completely traversed, return the top level's cumulative tally.
Code: (Demo)
$array = [10, [[[]],[]], 'hello', [1, 2, 3, ['hi', 7]], [15, 67], 12];
function array_tally_recursive(array $array): int {
$tally = 0;
foreach ($array as $item) {
if (is_array($item)) {
$tally = 1 array_tally_recursive($item);
}
}
return $tally;
}
echo array_tally_recursive($array);