Home Backend Development PHP Problem PHP array learning how to remove any element

PHP array learning how to remove any element

Aug 11, 2021 pm 06:21 PM
php Delete element array

In the previous article, we introduced the method of deleting the first element or the last element in the array. If you are interested, you can click on the link to view → "PHP Array Learning: How to Remove the First and Last Elements 》. This time we continue to introduce deleting array elements and show you how to delete any element of the array.

Most of the time, the elements that need to be deleted are not fixed. They are not necessarily the beginning or end of the array, nor do they necessarily delete only one. This makes it impossible to use the array_shift() and array_pop() functions. So how can we delete any element in the array? Let’s find out today.

We previously introduced the array_splice() function in "PHP Array Learning Skillful Use of Functions to Insert Elements (2)". It is a powerful function with multiple functions: it can be inserted Array elements can be replaced, and of course array elements can be deleted (after all, the job of the array_splice() function is to delete the specified element and replace it with other values). Let’s take a look at its deletion function.

Let’s look at a small example below.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24);
echo "原数组:";
var_dump($arr);

echo "删除后的数组:" ;
array_splice($arr,2);
var_dump($arr);
?>
Copy after login

The output result is:

PHP array learning how to remove any element

It can be seen that we use array_splice($arr,2)from the $arr array Elements are deleted starting from the 3rd element, and a total of 3 elements are deleted (all elements starting from the 3rd element are deleted). array_splice($arr,$start) will delete all elements starting from the $start position.

The $start parameter has three values:

  • is a positive number, then start from the $start position and delete later;

  • ## If
  • # is 0, then delete it starting from the first element; if

  • is a negative number, then start from the position

    -start from the end of $arr Start by deleting from the back to the front. For example -2 means start from the second to last element of the array.

  • <?php
    header("Content-type:text/html;charset=utf-8");
    $arr=array(10,12,20,25,24);
    echo "原数组:";
    var_dump($arr);
    
    echo "删除后的数组:" ;
    array_splice($arr,-2);
    var_dump($arr);
    ?>
    Copy after login
The output result is:

PHP array learning how to remove any element

array_splice() function is powerful and can delete multiple elements or only delete An element, then you need to specify a

$length parameter (the third parameter) to the function, which is used to specify the number of elements to be deleted.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24);
echo "原数组:";
var_dump($arr);

echo "删除后的数组:" ;
array_splice($arr,2,1);
var_dump($arr);
?>
Copy after login

The output result is:

PHP array learning how to remove any element

It can be seen that only the third element "20" is deleted.

In the operation of deleting elements, the $length parameter also has three values:

  • is a positive number, then it means deleting length elements;

  • is a negative number, then all elements starting from start and ending with length counting down from the end of the array will be deleted;

  • If it is omitted, then all elements starting from start will be deleted. All elements up to the end of the array.

  • <?php
    header("Content-type:text/html;charset=utf-8");
    $arr=array(10,12,20,25,24);
    echo "原数组:";
    var_dump($arr);
    
    echo "删除后的数组:" ;
    array_splice($arr,2,-1);
    var_dump($arr);
    ?>
    Copy after login
    The output result is:

    PHP array learning how to remove any element

    $lengthThe parameter can also be 0, which means no To delete an element, you can combine it with the fourth parameter $value of the function to perform an insertion operation (I will not give a detailed introduction here).

    Okay, that’s all. If you want to know anything else, you can click this. → →

    php video tutorial

    Finally, I would like to recommend a free video tutorial on PHP arrays:

    PHP function array array function video explanation, come and learn!

    The above is the detailed content of PHP array learning how to remove any element. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles