How to delete specified value in array in PHP

PHPz
Release: 2023-03-24 13:42:02
Original
1348 people have browsed it

In today's Internet world, PHP (PHP: Hypertext Preprocessor) is a popular scripting language used to develop web applications. A common task is to handle arrays in PHP, and the operation involved is to delete a specified value from the array. This article will introduce how to delete a specified value in an array in PHP.

What is a PHP array?

In PHP, an array is a data type that can store multiple values. Arrays can contain numbers, strings, or even other arrays. Arrays have the characteristics of an indexed array, where each element of the array has a unique index. PHP arrays can be declared in two ways:

// 声明一个空数组
$array = array();

// 包含值的声明数组
$array = array("apple", "banana", "orange");
Copy after login

Array elements can be created using numeric indexing and elements can be added to the array using the following syntax:

// 增加一个元素
$array[] = "apple";

// 增加多个元素
$array = array("apple", "banana", "orange");
array_push($array, "grape", "pear");
Copy after login

How to delete in PHP Specified value in array?

PHP provides some built-in functions to operate on arrays. One of them is array_splice(), which is used to delete the specified index and value in the array. For example, the following code demonstrates how to delete "banana" in an array containing multiple fruits:

$array = array("apple", "banana", "orange");
$index = array_search("banana", $array);
if($index !== false) {
  array_splice($array, $index, 1);
}
Copy after login
  • First, use the array_search() function to find what you want to delete in the array The index of the value.
  • array_splice()The first parameter of the function is the array to be operated on, the second parameter is the index from which to start the operation, and the third parameter is the number of elements to be deleted. In this case we are removing only one element, so the quantity is 1.
  • Finally, we check if array_search() returns false to ensure we successfully found the value to delete. If found successfully, we can delete it using array_splice().

Deleting elements in the array will change the index, so we need to use the array_values() function to re-index the array as follows:

$array = array_values($array);
Copy after login

Now, this The array will not contain "banana" and the indexes of the other elements will be adjusted to match the new array.

In addition to the array_splice() function, PHP also provides some other built-in functions to delete elements in an array, such as unset(). The sample code is as follows:

$array = array("apple", "banana", "orange");
$key = array_search("banana", $array);
if($key !== false) {
  unset($array[$key]);
}
Copy after login

Summary

This article introduces how to delete the specified value in the array in PHP. The key point is to use the array_splice() function to remove elements from an array, and the array_search() function to find the value to remove. Finally, we introduced another way to remove elements from an array using the unset() function.

If you need to remove specific elements when using arrays in PHP, please use the above method to do so. These methods make it easy to delete elements at any position in the array, thereby improving the efficiency of PHP development.

The above is the detailed content of How to delete specified value in array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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