An n-dimensional array that asks for the number of elements in each dimension (each element of each dimension may be an array or a scalar)

Source: Internet
Author: User
Tags scalar
No code, no idea, you can.
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'    ))

);
Back to 3,5,14

Reply content:

No code, no idea, you can.
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'    ))

);
Back to 3,5,14

I think you're going to have to do this sort of statistic, only in a recursive way.

// $array:要统计的数组,$i为第几维,$count维上层统计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 */

Well, I write a loop (optimized, self-optimizing, because I'm not very familiar with PHP)
In short, a breadth-first search (a slightly optimized queue, if changed to a stack, is the 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]
The third dimension of the number of elements is not arr{x}{y}.length it?
And so on?
Or do I have a mistake in understanding your problem?

With the square brackets just ahead of the not out,,,

$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)) ;大神解答,发出来给大家分享一下
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.