Home  >  Article  >  Backend Development  >  How to delete elements in an array in php

How to delete elements in an array in php

PHPz
PHPzOriginal
2023-04-25 18:26:54614browse

In PHP, deleting elements in an array is a very basic operation and is often used in development. Here are a few ways to do this.

Method 1: unset function
You can use PHP's built-in unset() function to delete elements in the array.

Sample code:

$fruits = array("apple", "banana", "orange", "grape");
unset($fruits[2]);
print_r($fruits);

Output result:

Array
(
    [0] => apple
    [1] => banana
    [3] => grape
)

Method 2: array_splice function
Another common method is to use PHP’s built-in array_splice() function. This function allows you to add or remove elements from an array.

Sample code:

$fruits = array("apple", "banana", "orange", "grape");
array_splice($fruits, 2, 1); 
print_r($fruits);

Output result:

Array
(
    [0] => apple
    [1] => banana
    [3] => grape
)

In the above example, the first parameter specifies the array to be modified, and the second parameter specifies the element to be deleted. The index, and the third parameter specifies the number of elements to remove. If the third argument is omitted, all elements from the specified index to the end of the array will be removed.

Method 3: array_diff and array_values ​​functions
Another method is to use a combination of array_diff() and array_values() functions. The array_diff() function returns the difference between two arrays, and the array_values() function returns all values ​​in an array, re-indexed with consecutive numeric indices.

Sample code:

$fruits = array("apple", "banana", "orange", "grape");
$remove = array("orange");
$fruits = array_values(array_diff($fruits, $remove));
print_r($fruits);

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => grape
)

In the above example, the first parameter specifies the array to be modified, and the second parameter specifies the element to be deleted. . The array_diff() function removes all elements from the first array that are in the second array. The elements in the returned array are then re-indexed into consecutive numeric indices using the array_values() function.

Summary
In PHP, deleting elements in an array is a very basic operation. The above methods may not be a complete list, but they are the most common ways to delete array elements using PHP. Which method you choose depends on your needs. No matter which method you use, always remember to test your code to make sure it works as expected.

The above is the detailed content of How to delete elements in an array in php. 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