Example of PHP getting the length of any array-PHP source code

Source: Internet
Author: User
PHP cannot use the count function provided by php to obtain the length of any array to implement custom development. Let's take a look at how to detect the length of any array, the specific operations are as follows. PHP cannot use the count function provided by php to obtain the length of any array to implement custom development. Let's take a look at how to detect the length of any array, the specific operations are as follows.

Script ec (2); script

PHP often processes arrays with unknown depth. In this case, recursion can be used, but it is better to know the array depth.

Obtain the array length:

The Code is as follows:

/**
* @ Description get the array Length
* @ Param array
* @ Return int length of array
*/
Function array_len ($ array = array ()){
$ Len = 1;

Foreach ($ array as & $ value ){
If (is_array ($ value )){
$ Len = array_len ($ value) + 1;
}
}
Return $ len;
}

Test:

The Code is as follows:

$ Arr = array (
Array (
Array (
Array (
Array ('id' => 1)
)
)
)
);
$ Arr_len = array_len ($ arr );

Var_dump ($ arr_len );

// Result

Int (5)

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.