PHP statistics one-dimensional array and two-dimensional array length

Source: Internet
Author: User

<?php Tutorial
$arr =array (
0=>array (' title ' => ' News 1 ', ' Viewnum ' => 123, ' content ' => ' ZAQXSWEDCRFV '),
1=>array (' title ' => ' News 2 ', ' Viewnum ' =>, ' content ' => ' qwertyuiopzxcvbnm ')
);
?>
If you want to count the length of the array $arr, which means that the two-dimensional array is only two news, the number you want is 2, but if you use the different versions of PHP with Count ($arr), the results are not the same;
Later found in the PHP manual, the Count function also has a second argument, which is explained as follows:
The Count function has two parameters:
0 (or Count_normal) is the default and does not detect multidimensional arrays (arrays in an array);
1 (or count_recursive) to detect multidimensional arrays,
So if you want to determine whether the read array $arr has news information, it should be written:
<?php
if (Is_array ($arr) && count ($arr, count_normal) >0)
{
.....
} else {
.....
}
?>
You can use this code to test the function:
<?php
$arr =array (
0=>array (' title ' => ' News 1 ', ' Viewnum ' => 123, ' content ' => ' ZAQXSWEDCRFV '),
1=>array (' title ' => ' News 2 ', ' Viewnum ' =>, ' content ' => ' qwertyuiopzxcvbnm ')
);
Echo ' does not count multidimensional arrays: '. Count ($arr, 0);//count ($arr, Count_normal)
echo "<br/>";
Echo ' statistical multidimensional array: '. Count ($arr, 1);//count ($arr, count_recursive)
?>
OK, so far, I've solved the problem of getting the first dimensional length of two-dimensional or multidimensional arrays in PHP!

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.