Home >Backend Development >PHP Problem >How to remove partial values ​​from an array in php

How to remove partial values ​​from an array in php

青灯夜游
青灯夜游Original
2022-06-02 20:08:161952browse

Two methods: 1. Use "array_splice($arr, position, number)" to delete the specified number of values ​​starting from the specified position. If the number of deletions is not set (the third parameter is omitted), then All values ​​starting at the specified position will be deleted. 2. Use "array_slice($arr, position)" to delete all values ​​before the specified position.

How to remove partial values ​​from an array in php

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

php remove the array part Two methods of value

Method 1: Use array_splice() function

array_splice() function can delete the specified number starting from the specified position Elements

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24,23,45,78,-1);
var_dump($arr);
echo "删除后的数组:" ;
array_splice($arr,2,4);
var_dump($arr);
?>

How to remove partial values ​​from an array in php

Method 2: Use the array_slice() function

array_slice() to intercept all elements starting at the specified position, That is, delete all elements before the specified position.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(10,12,20,25,24,23,45,78,-1);
var_dump($arr);
echo "删除后的数组:" ;
var_dump(array_slice($arr,2));
var_dump(array_slice($arr,4));
var_dump(array_slice($arr,6));
var_dump(array_slice($arr,8));
?>

How to remove partial values ​​from an array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove partial values ​​from an 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