Home Backend Development PHP Tutorial php数组中删除元素的实现代码_PHP

php数组中删除元素的实现代码_PHP

Jun 01, 2016 pm 12:12 PM
Delete element

复制代码 代码如下:
$arr = array('a','b','c','d');
unset($arr[1]);
print_r($arr);
?>

print_r($arr)之后,结果却不是那样的,最终结果是 Array ( [0] => a [2] => c [3] => d
那么怎么才能做到缺少的元素会被填补并且数组会被重新索引呢?答案是array_splice():
复制代码 代码如下:
$arr = array('a','b','c','d');
array_splice($arr,1,1);
print_r($arr); // Array ( [0] => a [1] => c [2] => d )
?>
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)

How to delete elements but keep child elements in jquery How to delete elements but keep child elements in jquery Nov 19, 2021 pm 02:22 PM

How to delete elements but keep child elements in jquery: 1. Use the children() method to get all the child elements of the specified element; 2. Use the unwrap() method to delete the parent element of the child element but keep the child elements. The syntax "$(" specifies the element. ").children().unwrap();".

How to delete elements from set in javascript How to delete elements from set in javascript Jan 12, 2022 am 10:56 AM

Methods to delete elements: 1. Use delete() to delete the specified element from the Set object, the syntax is "setObj.delete(value);"; 2. Use clear() to delete all elements in the Set object, the syntax is "setObj.delete(value);" "setObj.clear();".

How to delete an element with a specified key in a PHP array How to delete an element with a specified key in a PHP array Jul 07, 2023 pm 11:15 PM

How to delete an element with a specified key name in a PHP array In PHP development, array is a very important data structure. When operating an array, you often encounter situations where you need to delete elements with specified key names. This article will use code examples to introduce how to delete elements with specified key names in PHP arrays. PHP provides a variety of methods to delete elements in an array. Three of the commonly used methods are introduced below. Method 1: Use the unset() function The unset() function can be used to destroy specified variables, including elements in the array.

Use the array_pop() function in PHP to delete the last element of the array Use the array_pop() function in PHP to delete the last element of the array Nov 18, 2023 pm 12:45 PM

The array_pop() function in PHP is used to delete the last element of the array. This article will introduce the use of array_pop() function in detail and provide relevant code examples. In PHP, array is a very commonly used data structure used to store multiple related data. Sometimes, we may need to remove an element from the end of an array. At this time, you can use the array_pop() function to complete this operation. The syntax of the array_pop() function is as follows: mixedar

C++ program to remove the smallest element from both sides so that the 2* minimum value is greater than the maximum value C++ program to remove the smallest element from both sides so that the 2* minimum value is greater than the maximum value Aug 28, 2023 am 08:09 AM

The problem involves removing elements from either side of a list of integers in such a way that 2*min is greater than max. vector<int>arr={250,10,11,12,19,200};res=solve(arr);We can use brute force method. We can try all possible satisfactions and find the longest subarray that satisfies the 2*min>max condition. We can also use dynamic programming methods to try all possible excessive and unwanted subarray combinations. Example (using vector ADT) Let's say we have an array like "[250,10,11,12,19,200]". To get the best solution we need to remove elements [250,200

Is it necessary to use delete to delete elements in Go language? Is it necessary to use delete to delete elements in Go language? Mar 22, 2024 am 10:33 AM

In Go language, there is no need to use delete function to delete elements. In the Go language, we usually use slices to store elements. When we need to delete elements, we can do so through slice operations. The following will illustrate how to delete elements in Go language through specific code examples. First, we create a slice containing some elements and print out the initial slice content: packagemainimport(&quot;fmt&quot;)

How to add and remove elements from PHP array How to add and remove elements from PHP array Sep 05, 2023 pm 02:12 PM

How to add and remove elements from PHP arrays In PHP, arrays are a very common and important data structure. Arrays can hold multiple values ​​and can dynamically add or subtract elements as needed. This article will explain how to add and remove array elements in PHP and provide corresponding code examples. 1. Add elements using square brackets [] syntax. The easiest way to add elements is to use square brackets [] syntax. An example is as follows: $arr=["apple",&quo

How to remove the last array element in php How to remove the last array element in php Sep 17, 2021 pm 07:09 PM

Two removal methods: 1. Use the array_pop() function to delete elements at the end of the array, with the syntax "array_pop ($arr)"; 2. Use the array_splice() function to delete part of the elements of the array. You only need to add the second Just set the parameter to "-1", the syntax is "array_splice($arr,-1)".

See all articles