PHP array calculation
Array is the most commonly used type, so how to calculate the number of a one-dimensional array. In fact, we can use one of the mathematical functions we learned before: count().
Let’s take a look at the usage of the count function:
int count (mixed $ variable)
Note:
1. The parameter $ variable is required to be an array or an object that can be counted
Then we can try to use the statistical function to count the number of arrays.
'百度', 'ali' => '阿里', 'tencent' => '腾讯', ]; echo count($data); $erwei = [ [ 'baidu' =>'百度', 'ali' => '阿里', 'tencent' => '腾讯', ], [ 'netease' =>'网易', 'sohu' => '搜狐', 'sina' => '新浪', ] ]; //试试输出一个二维数组个数 echo count($erwei); //试试输出二维数组中某个元素的个数 echo count($erwei[1]); ?>
Through the above example, we found that both the index array and the number of associative arrays can be output.
If it is a two-dimensional array, this function will only count the number of array elements in the current dimension. As in the above example: $erwei.
So the result of count($erwei) is 2. When counting($erwei[1]), the result is 3.