How to remove specified elements from an array in php

青灯夜游
Release: 2023-03-08 18:12:02
Original
2629 people have browsed it

php method to remove specified elements from an array: 1. Use array_splice() function, syntax "array_splice(array,start,length,array)"; 2. Use unset() function, syntax "unset (var) ".

How to remove specified elements from an array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php removes the array specification Element

Method one:

Copy after login

Output:

array(4) { [0]=> int(1) [1]=> int(5) [2]=> int(7) [3]=> int(8) }
Copy after login

Method two:

$value)
{
  if ($value === 3)
    unset($arr2[$key]);
}
var_dump($arr2);
?>
Copy after login

Output:

array(4) { [0]=> int(1) [2]=> int(5) [3]=> int(7) [4]=> int(8) }
Copy after login

Summary:

You can see that there is a difference between using array_splice() to delete specific values ​​and using unset to delete specific values.

If the array_splice() function is deleted, the index value of the array will also change.

If the unset() function is deleted, the index value of the array will not change.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove specified elements from an array in php. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!