How to remove multiple elements from php array

青灯夜游
Release: 2023-03-16 07:46:02
Original
2890 people have browsed it

In PHP, array_splice() can be used to remove multiple array elements. This function can delete a specified number of elements starting from a specified position. The syntax is "array_splice(array, starting position, number of deleted)". If the second parameter is a positive array, it will be deleted from front to back; if it is a negative number, it will be deleted from the reciprocal position.

How to remove multiple elements from php array

The operating environment of this tutorial: Windows 7 system, PHP 8.1 version, Dell G3 computer.

In php, you can use the array_splice() function to remove multiple array elements.

array_splice() function can delete the specified number of elements starting from the specified position. This function changes the original array. Grammar:

array_splice(array,start,length)
Copy after login
  • arr represents an array.
  • start indicates the position (subscript) where deletion starts:
    •           If start is a positive number, delete from front to back.
    •             If start is a negative number, start from the position -start from the end of arr and delete it from the back to the front. For example -2 means start from the second to last element of the array.
  • length is an optional parameter, indicating the number of elements to delete:
    •           If length is a positive number, it means deleting length elements;
    •           If length is a negative number, then all elements starting from start and counting down to length at the end of the array will be deleted;
    •                    If omitted, all elements starting at start and ending at the end of the array will be deleted.

If start is a positive number, you need to specify the number of deleted elements length; if start is a negative number, count down from Delete the specified number of elements starting from the position.

Example 1: Remove 2 elements from the beginning

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

echo "从开头删除2个元素";
array_splice($arr,0,2); 
var_dump($arr); 
?>
Copy after login

How to remove multiple elements from php array

Example 2: Remove 2 from the middle elements

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

echo "从中间删除2个元素";
array_splice($arr,2,2); 
var_dump($arr); 
?>
Copy after login

How to remove multiple elements from php array

Example 3: Remove 2 elements from the end

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

echo "从末尾删除2个元素";
array_splice($arr,-2); 
var_dump($arr); 
?>
Copy after login

How to remove multiple elements from php array

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove multiple elements from php array. 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