PHP Array is a very common data type that we often use in programming. An array is a container that can store multiple values, and they can be created and accessed in many different ways. This article will introduce some commonly used PHP array calling methods.
1. Create an array
In PHP, we can create an array in two ways. One is created through the array() function, and the other is created using square brackets [].
//使用 array() 函数 $my_array = array('apple', 'banana', 'orange'); //使用中括号 [] $my_array = ['apple', 'banana', 'orange'];
2. Accessing array elements
We can use array subscripts to access elements in the array. PHP array subscripts can be numbers or strings.
$my_array = ['apple', 'banana', 'orange']; //使用数字下标 echo $my_array[0]; //输出:apple echo $my_array[1]; //输出:banana //使用字符串下标 $my_array = [ 'name' => 'Tom', 'age' => 18, 'gender' => 'male' ]; echo $my_array['name']; //输出:Tom echo $my_array['age']; //输出:18
If we access a non-existent index, an Undefined index error will occur. To avoid this error, we can use isset() to check whether the subscript exists.
$my_array = [ 'name' => 'Tom', 'age' => 18 ]; if (isset($my_array['gender'])) { echo $my_array['gender']; } else { echo 'gender is not set.'; }
3. Modify array elements
Modifying array elements is very simple, just use array subscripts to assign new values.
$my_array = [ 'name' => 'Tom', 'age' => 18 ]; $my_array['age'] = 20; //将 age 改为 20 echo $my_array['age']; //输出:20
4. Delete array elements
You can use the unset() function to delete elements in the array.
$my_array = [ 'name' => 'Tom', 'age' => 18 ]; unset($my_array['age']); //删除 age 元素 print_r($my_array); //输出:Array ( [name] => Tom )
5. Traversing the array
Traversing the array is a common operation, we can use for loop, foreach loop, etc. to achieve it.
//使用 for 循环遍历数组 $my_array = ['apple', 'banana', 'orange']; for ($i = 0; $i < count($my_array); $i++) { echo $my_array[$i] . ' '; } //输出:apple banana orange //使用 foreach 循环遍历关联数组 $my_array = [ 'name' => 'Tom', 'age' => 18, 'gender' => 'male' ]; foreach ($my_array as $key => $value) { echo $key . ':' . $value . ' '; } //输出:name:Tom age:18 gender:male
6. Array sorting
Array sorting can use sort(), rsort(), asort(), arsort(), ksort(), krsort() functions, etc. For specific usage of these functions, please refer to the PHP official documentation.
//使用 sort() 函数对数字数组进行升序排序 $my_array = [3, 1, 4, 1, 5, 9, 2, 6, 5]; sort($my_array); //升序排序 print_r($my_array); //输出:Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 5 [7] => 6 [8] => 9 ) //使用 asort() 函数对关联数组进行升序排序 $my_array = [ 'apple' => 5, 'banana' => 2, 'orange' => 3 ]; asort($my_array); //升序排序 print_r($my_array); //输出:Array ( [banana] => 2 [orange] => 3 [apple] => 5 )
7. Array Merge
You can use the array_merge() function to merge two arrays into one array.
$my_array1 = ['apple', 'banana']; $my_array2 = ['orange', 'pear']; $new_array = array_merge($my_array1, $my_array2); print_r($new_array); //输出:Array ( [0] => apple [1] => banana [2] => orange [3] => pear )
Summary
The above are some calling methods of PHP arrays. Arrays are a very important data type in our development process. Proficient mastery of array operations can improve our coding efficiency. .
The above is the detailed content of Some commonly used PHP array calling methods. For more information, please follow other related articles on the PHP Chinese website!