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

How to add and remove elements from PHP array

Sep 05, 2023 pm 02:12 PM
Delete element Array operations Add element

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:

1

2

3

$arr = ["apple", "banana", "orange"];

$arr[] = "grape";

print_r($arr);

Copy after login

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:

1

2

3

4

5

6

7

Array

(

    [0] => apple

    [1] => banana

    [2] => orange

    [3] => grape

)

Copy after login
  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:

1

2

3

$arr = ["apple", "banana", "orange"];

array_push($arr, "grape", "watermelon");

print_r($arr);

Copy after login

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:

1

2

3

4

5

6

7

8

Array

(

    [0] => apple

    [1] => banana

    [2] => orange

    [3] => grape

    [4] => watermelon

)

Copy after login

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:

1

2

3

$arr = ["apple", "banana", "orange"];

unset($arr[1]);

print_r($arr);

Copy after login

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:

1

2

3

4

5

Array

(

    [0] => apple

    [2] => orange

)

Copy after login

    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:

1

2

3

$arr = ["apple", "banana", "orange"];

array_splice($arr, 1, 1);

print_r($arr);

Copy after login

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:

1

2

3

4

5

Array

(

    [0] => apple

    [1] => orange

)

Copy after login

    Use
  1. array_pop() The function
can be used

array_pop() Function deletes the last element in the array. An example is as follows:

1

2

3

$arr = ["apple", "banana", "orange"];

array_pop($arr);

print_r($arr);

Copy after login

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:

1

2

3

4

5

Array

(

    [0] => apple

    [1] => banana

)

Copy after login
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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete elements but keep child elements in jquery How to delete elements but keep child elements in jquery Nov 19, 2021 pm 02:22 PM

How to delete elements but keep child elements in jquery

How to put string into array in PHP and split by newline How to put string into array in PHP and split by newline Aug 28, 2023 pm 10:57 PM

How to put string into array in PHP and split by newline

Merge operation of arrays in PHP8.0: array_merge Merge operation of arrays in PHP8.0: array_merge May 14, 2023 am 08:52 AM

Merge operation of arrays in PHP8.0: array_merge

How to delete elements from set in javascript How to delete elements from set in javascript Jan 12, 2022 am 10:56 AM

How to delete elements from set in javascript

Dangerous operations on arrays in PHP8.0: array_splice() Dangerous operations on arrays in PHP8.0: array_splice() May 14, 2023 am 08:24 AM

Dangerous operations on arrays in PHP8.0: array_splice()

Use PHP custom functions to extend the functionality of array intersection and union Use PHP custom functions to extend the functionality of array intersection and union May 01, 2024 am 10:45 AM

Use PHP custom functions to extend the functionality of array intersection and union

How to delete an element with a specified key in a PHP array How to delete an element with a specified key in a PHP array Jul 07, 2023 pm 11:15 PM

How to delete an element with a specified key in a PHP array

Best Practices for Array Operations with PHP Best Practices for Array Operations with PHP Jun 06, 2023 am 10:30 AM

Best Practices for Array Operations with PHP

See all articles