Home>Article>Backend Development> How to delete specified value in array in PHP

How to delete specified value in array in PHP

PHPz
PHPz Original
2023-03-24 12:57:42 1290browse

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");

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");

How to delete in PHP Specified value in array?

PHP provides some built-in functions to operate on arrays. One of them isarray_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); }
  • First, use thearray_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 ifarray_search()returns false to ensure we successfully found the value to delete. If found successfully, we can delete it usingarray_splice().

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

$array = array_values($array);

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 thearray_splice()function, PHP also provides some other built-in functions to delete elements in an array, such asunset(). The sample code is as follows:

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

Summary

This article introduces how to delete the specified value in the array in PHP. The key point is to use thearray_splice()function to remove elements from an array, and thearray_search()function to find the value to remove. Finally, we introduced another way to remove elements from an array using theunset()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!

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