The array function is one of the most commonly used functions in PHP. It can be used to create, operate and manage arrays. When developing applications, using array functions can greatly improve development efficiency. This article will introduce some basic usage and examples of array functions in PHP to help everyone better understand and master array functions.
1. Creation and initialization of arrays
Arrays in PHP can be created in the following ways:
//通过array()函数创建数组 $arr = array("a", "b", "c"); //通过[]运算符创建数组 $arr = ["a", "b", "c"];
Arrays created by the above two methods are index arrays. The subscripts increase sequentially starting from 0, or you can manually specify the subscripts, as shown below:
$arr1 = array(0 => "a", 1 => "b", 2 => "c"); $arr2 = [0 => "a", 1 => "b", 2 => "c"];
PHP also supports associative arrays, that is, you can use strings as subscripts, as shown below:
$arr = array("name" => "tom", "age" => 20);
The above are the basic creation and initialization methods of arrays. Next, we will introduce several commonly used array functions.
2. Counting the number of array elements
Counting the number of array elements is a common requirement in the development process. PHP provides two functions to implement this function: count() and sizeof( ). Their functions are the same and their usage is basically the same. Look at the following example:
//定义数组 $arr = array("a", "b", "c"); //使用count()函数统计数组元素个数 $num = count($arr); echo $num; // 输出:3 //使用sizeof()函数统计数组元素个数 $num = sizeof($arr); echo $num; // 输出:3
In the above code, count() and sizeof() have the same usage, and the return value is the number of array elements.
3. Traversing array elements
Traversing array elements is also one of the most frequently used functions of PHP arrays. The following are two methods of traversing an array:
//定义数组 $arr = array("a", "b", "c"); //遍历数组元素 for($i=0; $i<count($arr); $i++){ echo $arr[$i]."<br/>"; }
The above code uses a for loop to traverse the array elements, the effect is as follows:
a b c
//定义数组 $arr = array("name" => "tom", "age" => 20, "hobby" => "running"); //遍历数组元素 foreach($arr as $key => $value){ echo $key." : ".$value."<br/>"; }
The above code uses foreach to traverse the array elements, and the output result is as follows:
name : tom age : 20 hobby : running
4. Array sorting
Array sorting is in A commonly used operation in development, in PHP, array sorting is divided into two types: index array sorting and associative array sorting.
//定义数组 $arr = array("d", "a", "c", "b"); //使用sort()函数进行排序 sort($arr); print_r($arr); // 输出:Array ( [0] => a [1] => b [2] => c [3] => d )
The above code uses the sort() function to sort the index array, and the returned result is an array in ascending order.
Associative array sorting, if you need to sort keywords, you can use the ksort() or asort() function. The ksort() function sorts in ascending order of keywords, while asort() sorts in descending order of keywords. Let’s look at two examples:
//定义数组 $arr = array("name" => "tom", "age" => 20, "hobby" => "running"); //使用ksort()函数按照关键字的升序进行排序 ksort($arr); print_r($arr); // 输出:Array ( [age] => 20 [hobby] => running [name] => tom ) //使用asort()函数按照关键字的降序进行排序 asort($arr); print_r($arr); // 输出:Array ( [name] => tom [hobby] => running [age] => 20 )
In the above code, the ksort() and asort() functions sort according to the ascending and descending order of keywords respectively.
5. Array deduplication
In addition to sorting, the problem of duplicate elements in the array also needs to be solved. Two commonly used deduplication functions are introduced below.
array_unique() function can remove duplicate elements in the array, as shown below:
//定义数组,包含重复元素 $arr = array(1, 2, 3, 1, 2); //使用array_unique()函数去重 $arr = array_unique($arr); print_r($arr); // 输出:Array ( [0] => 1 [1] => 2 [2] => 3 )
In the above code, array_unique () function removes duplicate elements from the array and returns a new array.
array_keys() function can return all the key names in the array. Call the array_keys() function once and compare its return value with the original array. By comparison, you can get a new array with duplicate elements removed. Let’s look at an example:
//定义数组,包含重复元素 $arr = array(1, 2, 3, 1, 2); //使用array_keys()函数去重 $arr = array_keys(array_flip($arr)); print_r($arr); // 输出:Array ( [0] => 1 [1] => 2 [2] => 3 )
In the above code, the array_flip() function returns a new array with the array keys and values exchanged, and the array_keys() function returns all the key names in the new array.
The above is the basic knowledge of PHP array functions. Although these functions are simple and easy to use, their application range is very wide. In actual development, appropriate array functions need to be used according to actual needs in order to take advantage of their advantages and value.
The above is the detailed content of PHP basic tutorial array function. For more information, please follow other related articles on the PHP Chinese website!