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.

<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
// $result == 3

$b[0]  = '迪奥和奥迪我都爱';
$b[5]  = '努力开创未来';
$b[10] = '为了未来而努力';
$result = count($b);


$data = [
       'baidu' =>'百度',
       '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.


Continuing Learning
||
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
// $result == 3
$b[0] = '';
$b[5] = '';
$b[10] = '';
$result = count($b);
$data = [
'baidu' =>'',
'ali' => '',
'tencent' => '',
];
echo count($data);
$erwei = [
[
'baidu' =>'',
'ali' => '',
'tencent' => '',
],
[
'netease' =>'',
'sohu' => '',
'sina' => '',
]
];
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
submitReset Code
图片放大关闭