PHP is a programming language widely used in the field of web development. Among them, array is a very important data structure that can be used in a variety of scenarios. However, when the array needs to be changed, some difficulties may be encountered. This article will introduce the methods and precautions for changing PHP arrays.
1. The definition and characteristics of PHP arrays
In PHP, an array is an ordered collection used to store multiple values. Among them, the array consists of keys and values. Keys can be numbers or strings, and values can be any type of data. A basic array definition is as follows:
$fruit = array('apple', 'banana', 'orange');
You can also use square brackets [] to represent an array:
$fruit = ['apple', 'banana', 'orange'];
The array keys can be specified manually, as follows:
$student = array( 'name' => 'Tom', 'age' => 20, 'gender' => 'Male' );
2. Add elements to the array
In PHP, there are many ways to add elements to the array:
1. Use the array_push() function to add elements to the end of the array
$fruit = ['apple', 'banana', 'orange']; array_push($fruit, 'pear'); //向数组末尾添加'pear' print_r($fruit); //Array([0] => apple [1] => banana [2] => orange [3] => pear)
2 .Use the [] operator of the array to add elements to the end of the array
$fruit = ['apple', 'banana', 'orange']; $fruit[] = 'pear'; //向数组末尾添加'pear' print_r($fruit); //Array([0] => apple [1] => banana [2] => orange [3] => pear)
3.Use the array_unshift() function to add elements to the head of the array
$fruit = ['apple', 'banana', 'orange']; array_unshift($fruit, 'pear'); //向数组头部添加'pear' print_r($fruit); //Array([0] => pear [1] => apple [2] => banana [3] => orange)
4.Use the operator to merge the two arrays
$fruit1 = ['apple', 'banana']; $fruit2 = ['orange', 'pear']; $fruit = $fruit1 + $fruit2; //将$fruit1和$fruit2合并成一个数组 print_r($fruit); //Array([0] => apple [1] => banana [2] => orange [3] => pear)
Note: When using operators to merge two arrays, if there are elements with the same key name in the two arrays, the elements in the later array will overwrite the elements in the previous array.
3. Delete elements from the array
Similarly, there are many ways to delete array elements in PHP:
1. Use the array_pop() function to delete the element at the end of the array
$fruit = ['apple', 'banana', 'orange']; array_pop($fruit); //删除末尾元素'orange' print_r($fruit); //Array([0] => apple [1] => banana)
2. Use the array_shift() function to delete the array head element
$fruit = ['apple', 'banana', 'orange']; array_shift($fruit); //删除头部元素'apple' print_r($fruit); //Array([0] => banana [1] => orange)
3. Use the unset() function to delete the specified element
$fruit = ['apple', 'banana', 'orange']; unset($fruit[1]); //删除索引为1的元素'banana' print_r($fruit); //Array([0] => apple [2] => orange)
4. Modify the array elements
Modify the array elements It is one of the most common and easiest operations in PHP array operations. Array elements can be modified directly by specifying array key names.
$student = array( 'name' => 'Tom', 'age' => 20, 'gender' => 'Male' ); $student['age'] = 21; //将年龄更改为21 print_r($student); //Array([name] => Tom [age] => 21 [gender] => Male)
5. Determine whether the array element exists
Before modifying the array element, it is usually necessary to determine whether the element exists. This can be achieved using the in_array() or array_key_exists() function.
1. Use the in_array() function to determine whether the value exists
$fruit = ['apple', 'banana', 'orange']; if (in_array('banana', $fruit)) { echo "banana exists"; } else { echo "banana does not exist"; } //banana exists
2. Use the array_key_exists() function to determine whether the key exists
$student = array( 'name' => 'Tom', 'age' => 20, 'gender' => 'Male' ); if (array_key_exists('name', $student)) { echo "name exists"; } else { echo "name does not exist"; } //name exists
6. Summary
PHP array The operation is very flexible, and operations such as changing, adding, and deleting arrays can be implemented in various ways. When operating an array, you need to pay attention to the key names and key values of the elements to avoid operation failures due to duplicate key names and other issues. At the same time, before modifying an element, you need to determine whether the element exists to avoid program running errors due to non-existence. Mastering the operation skills of PHP arrays is very important for web development engineers.
The above is the detailed content of How to change elements in php array. For more information, please follow other related articles on the PHP Chinese website!