You can also use $ eartharray ({code...}) without code, or return 3, 5, and 14 without code.
For example
$ Earth = array (
'asia'=>array( 'china'=>array( 'beijing', 'shanghai', 'tianjin', 'hangzhou' ), 'japan'=>array( 'tokyo', 'hokkaido', 'nagasaki' )),'europe'=>array( 'france'=>array( 'paris', 'lyon' ), 'spain'=>array( 'madrid', 'barcelona', 'sevilla' )),'australia'=>array( 'australia'=>array( 'sydney', 'melbourne' ))
);
Returns 3, 5, 14
Reply content:
You can do this without code or ideas.
For example
$ Earth = array (
'asia'=>array( 'china'=>array( 'beijing', 'shanghai', 'tianjin', 'hangzhou' ), 'japan'=>array( 'tokyo', 'hokkaido', 'nagasaki' )),'europe'=>array( 'france'=>array( 'paris', 'lyon' ), 'spain'=>array( 'madrid', 'barcelona', 'sevilla' )),'australia'=>array( 'australia'=>array( 'sydney', 'melbourne' ))
);
Returns 3, 5, 14
I think you should only use recursion to make such statistics.
// $ Array: array to be counted. $ I indicates the dimension. $ count indicates the upper-layer function count_array ($ array, $ I = 1, $ count = array ()) {$ n = 0; if (isset ($ count [$ I]) {$ n = $ count [$ I];} $ count [$ I] = count ($ array) + $ n; $ I + = 1; foreach ($ array as $ item) {if (is_array ($ item )) {$ count = sp_arr ($ item, $ I, $ count) ;}} return $ count ;} $ array = array ('A' => array ('B' => 'C', 'D' => 'E ', 'F' => 'G',), 'H' => array ('I' => array ('J' => 'k ', 'L' => 'M', 'n' => array ('O' => 'P', 'q' => 'R ')))); $ count = count_array ($ array); var_dump ($ count); // result:/*** array (size = 4) * 1 => int 2*2 => int 4*3 => int 3*4 => int 2 */
Okay, I write a loop (it can be optimized and optimized by myself, because I am not very familiar with PHP)
In short, it is a breadth-first search (a queue with a slight optimization, if changed to a stack, It is a depth-first)
function count_array($array) { $count = array(); $level = 0; $tfifo = $array; do{ $fifo = $tfifo; $tfifo = array(); $count[$level] = 0; foreach($fifo as $item){ $count[$level]++; if(is_array($item)) { foreach($item as $subitem){ $tfifo[] = $subitem; } } } $level++; }while(count($tfifo)>0); return $count;}
Arr {x} {y} [z]
Isn't the number of elements in the third dimension arr {x} {y}. length?
And so on?
Or is there an error in my understanding of your problem?
Not listed in brackets ,,,
$ Num = array (); function get_num ($ fat, $ num) {$ num [] = count ($ fat); $ sun = array (); foreach ($ fat as $ key = >$ value) {if (is_array ($ value) {foreach ($ value as $ k => $ v) {$ sun [] = $ v ;}}if ($ sun) {return get_num ($ sun, $ num) ;}return $ num ;} // var_dump (get_num ($ arr, $ fat)