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

PHP array learning how to remove any element

青灯夜游
Release: 2023-03-12 06:16:01
Original
3348 people have browsed it

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!

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