Home  >  Article  >  Backend Development  >  How to add and remove elements from PHP array

How to add and remove elements from PHP array

王林
王林Original
2023-09-05 14:12:361035browse

PHP 数组如何添加和删除元素

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

  1. Use square brackets [] syntax

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
)
  1. Use array_push() function

PHP 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

    Use
  1. unset() function
You can use the

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
)

    Use the
  1. array_splice() 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
)

    Use
  1. array_pop() The function
can be used

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.

I hope this article will be helpful in understanding adding and removing elements from PHP arrays. For more knowledge about PHP arrays, you can refer to the official PHP documentation or related tutorials.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn