Home Backend Development PHP Problem How to delete elements in an array in php

How to delete elements in an array in php

Apr 25, 2023 pm 06:26 PM

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);
Copy after login

Output result:

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

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);
Copy after login

Output result:

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

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);
Copy after login

Output result:

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

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 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)