Home > Article > Backend Development > How to add and remove elements from PHP array
How to add and delete elements from PHP arrays
In PHP, arrays are a very common and important data structure. Arrays can hold multiple values and can dynamically add or subtract elements as needed. This article explains how to add and remove array elements in PHP and provides corresponding code examples.
1. Add elements
The easiest way to add elements is to use square brackets [] syntax. An example is as follows:
$arr = ["apple", "banana", "orange"]; $arr[] = "grape"; print_r($arr);
This code will add a new element "grape"
to the original array $arr
. Use print_r()
function to print the array, the output is as follows:
Array ( [0] => apple [1] => banana [2] => orange [3] => grape )
array_push()
functionPHP also provides The array_push()
function is available, which can add one or more elements to the end of the array at a time. The example is as follows:
$arr = ["apple", "banana", "orange"]; array_push($arr, "grape", "watermelon"); print_r($arr);
This code will add two new elements "grape"
and "watermelon"# to the original array
$arr ##. Use
print_r() function to print the array, the output is as follows:
Array ( [0] => apple [1] => banana [2] => orange [3] => grape [4] => watermelon )2. Delete elements
function
unset() function to delete one or more elements in the array. An example is as follows:
$arr = ["apple", "banana", "orange"]; unset($arr[1]); print_r($arr);This code will delete the element
"banana" with index
1 in the array
$arr. Use the
print_r() function to print the array, the output is as follows:
Array ( [0] => apple [2] => orange )
function
The array_splice() function can implement more complex array element deletion operations, and can delete, replace or insert elements in the array. An example is as follows:
$arr = ["apple", "banana", "orange"]; array_splice($arr, 1, 1); print_r($arr);This code will delete the element
"banana" with index
1 in the array
$arr. Use the
print_r() function to print the array, the output is as follows:
Array ( [0] => apple [1] => orange )
The function
array_pop() Function deletes the last element in the array. An example is as follows:
$arr = ["apple", "banana", "orange"]; array_pop($arr); print_r($arr);This code will delete the last element
"orange" in the array
$arr. Use the
print_r() function to print the array, the output is as follows:
Array ( [0] => apple [1] => banana )Summary: This article introduces how to add and delete array elements in PHP, and provides Corresponding code example. Through these methods, we can easily operate arrays in PHP and implement dynamic addition and subtraction of elements to the array. Whether using square brackets [] syntax,
array_push() function,
unset() function,
array_splice() function or
array_pop() function can easily add and delete elements to an array, improving the efficiency and flexibility of PHP development.
The above is the detailed content of How to add and remove elements from PHP array. For more information, please follow other related articles on the PHP Chinese website!