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

How to delete elements in an array in php

PHPz
Release: 2023-04-23 09:58:44
Original
1197 people have browsed it

PHP is a widely used server-side programming language with numerous functions and syntax features. Deleting array elements is a common task for PHP developers. This article will introduce several methods to delete array elements in PHP.

1. Use the unset() function

In PHP, you can use the unset() function to delete specified elements in the array. The syntax of the unset() function is very simple. You only need to pass the element to be deleted to the function in the form of a parameter. For example:

$myArray = array("apple", "banana", "orange", "grape");
unset($myArray[1]);
Copy after login

The above code will delete the second element (subscript 1) in the $myArray array, which is "banana".

It should be noted that using the unset() function will only delete the specified element, but it will not modify the key name of the array. If you need to reorder the keys of array elements, you can use the array_values() function to achieve this. For example:

$myArray = array("apple", "banana", "orange", "grape");
unset($myArray[1]);
$myArray = array_values($myArray);
Copy after login

The above code will delete the second element (subscript 1) in the $myArray array, and then reorder the keys of the remaining elements.

2. Use the array_splice() function

Another way to delete array elements is to use the array_splice() function. This function deletes elements from an array at a specified starting index and length. The following is the syntax of this function:

array_splice($array, $start, $length)
Copy after login

Among them, $array represents the array of elements to be deleted, $start represents the starting subscript, $length indicates the number of elements to be deleted.

For example, the following code deletes the two elements in the $myArray array whose subscripts start from 1 (i.e., "banana" and "orange"):

$myArray = array("apple", "banana", "orange", "grape");
array_splice($myArray, 1, 2);
Copy after login

After deletion, only "apple" and "grape" are left in the $myArray array.

3. Use the array_diff() function

In addition to the above two methods, you can also use the array_diff() function to delete elements from the array. This function creates a new array containing values ​​that are not in the specified array.

The following is the syntax of this function:

$newArray = array_diff($array, $values);
Copy after login

Among them, $array represents the original array, and $values represents the elements to be deleted. $newArray is the new array after deleting the specified element.

For example, the following code creates a new array that does not contain "banana" and "orange" in the $myArray array:

$myArray = array("apple", "banana", "orange", "grape");
$newArray = array_diff($myArray, array("banana", "orange"));
Copy after login

Finally, $newArrayThe array only contains "apple" and "grape".

Conclusion

The above are several ways to delete array elements in PHP. Use the unset() function to delete specified elements, while the array_splice() function can delete elements based on subscript and length. Finally, the array_diff() function can create a new array that does not contain the specified element. For PHP developers, mastering these methods can make them operate arrays more efficiently.

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template