Array is a commonly used data structure, and it is also widely used in PHP. In PHP, arrays can store one or more values and provide quick and easy access to these values. This article will introduce the usage of arrays in PHP, including how to create, access, operate and traverse arrays.
1. Create an array
In PHP, there are two ways to create an array: one is to use the array() function, and the other is to use square brackets [].
Use array() function:
$array1 = array(); // 创建一个空数组 $array2 = array(1, 2, 3); // 创建一个包含3个元素的数组 $array3 = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry'); // 创建一个包含3个键值对的关联数组
Use square brackets []:
$array4 = []; // 创建一个空数组 $array5 = [1, 2, 3]; // 创建一个包含3个元素的数组,PHP 5.4以上版本支持 $array6 = ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry']; // 创建一个包含3个键值对的关联数组,PHP 5.4以上版本支持
2. Access array elements
In PHP, you can use the following to access elements in an array. For ordinary arrays, the subscript starts from 0, and for associative arrays, the key name is used as the subscript. You can use square brackets [] or curly brackets {} to access array elements.
// 访问普通数组元素 $array = array(1, 2, 3); echo $array[0]; // 输出1 // 访问关联数组元素 $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry'); echo $fruit['a']; // 输出apple // 使用花括号{}访问数组元素 echo $fruit{'a'}; // 输出apple
3. Array operations
PHP provides some built-in functions for operating on arrays. Here are some commonly used array operation functions.
1. Add elements
// 使用[]语法添加元素 $array = array(1, 2, 3); $array[] = 4; // 添加一个元素 print_r($array); // 输出Array([0] => 1, [1] => 2, [2] => 3, [3] => 4) // 使用array_push()函数添加元素 $stack = array("orange", "banana"); array_push($stack, "apple", "raspberry"); print_r($stack); // 输出Array([0] => orange, [1] => banana, [2] => apple, [3] => raspberry)
2. Delete elements
// 使用unset()函数删除元素 $array = array(1, 2, 3); unset($array[1]); // 删除下标为1的元素 print_r($array); // 输出Array([0] => 1, [2] => 3) // 使用array_pop()函数删除最后一个元素 $stack = array("orange", "banana", "apple"); $fruit = array_pop($stack); // 删除最后一个元素,并返回删除的元素 print_r($stack); // 输出Array([0] => orange, [1] => banana) echo $fruit; // 输出apple
3. Merge arrays
// 使用+运算符合并数组,相同下标的元素使用右边数组的元素值 $array1 = array('a', 'b', 'c'); $array2 = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry'); $array3 = $array1 + $array2; print_r($array3); // 输出Array([0] => a, [1] => b, [2] => c, [a] => apple, [b] => banana, [c] => cherry) // 使用array_merge()函数合并数组,相同下标的元素使用后面数组的元素值,键值对保留 $array1 = array('a', 'b', 'c'); $array2 = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry'); $array3 = array_merge($array1, $array2); print_r($array3); // 输出Array([0] => a, [1] => b, [2] => c, [a] => apple, [b] => banana, [c] => cherry)
4. Sort
// 使用sort()函数对数组进行升序排序 $array = array(3, 1, 2); sort($array); print_r($array); // 输出Array([0] => 1, [1] => 2, [2] => 3) // 使用asort()函数对关联数组进行升序排序,键值对保留 $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry'); asort($fruit); print_r($fruit); // 输出Array([a] => apple, [b] => banana, [c] => cherry)
four , Traversing the array
In PHP, you can use the foreach loop to traverse the array. The foreach loop will iterate through each element in the array and assign it to the specified variable. For a normal array, the variable will hold the value of each element, and for an associative array, the variable will hold the key name and key value of each key-value pair.
// 遍历普通数组 $array = array(1, 2, 3); foreach ($array as $value) { echo $value . ' '; // 输出1 2 3 } // 遍历关联数组 $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry'); foreach ($fruit as $key => $value) { echo $key . ': ' . $value . ' '; // 输出a: apple b: banana c: cherry }
5. Summary
This article introduces the basic usage of arrays in PHP, including creating, accessing, operating and traversing arrays. Proficient in the usage of arrays will help improve PHP programming efficiency.
The above is the detailed content of In-depth analysis of the usage of arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!