Home > Backend Development > PHP Problem > How to delete specified elements in an array in php

How to delete specified elements in an array in php

王林
Release: 2023-03-06 20:16:02
Original
3493 people have browsed it

php method to delete specified elements in an array: You can use the array_splice() function to delete, such as [array_splice($array, 1, 1);]. The array_splice() function removes selected elements from an array and replaces them with new elements.

How to delete specified elements in an array in php

Function introduction:

(Video tutorial sharing: php video tutorial)

array_splice( ) function removes a selected element from an array and replaces it with a new element. The function will also return an array of removed elements.

Tip: If the function does not remove any elements (length=0), the replacement array will be inserted from the position of the start parameter.

Note: Key names in the replacement array are not retained.

Grammar:

array_splice(array,start,length,array)
Copy after login

Code implementation:

<?php
$array = array(0 => "a", 1 => "b", 2 => "c");
array_splice($array, 1, 1);
//↑ Offset which you want to delete
print_r($array);
?>
Copy after login

Output result:

Array(
    [0] => a    
    [1] => c
)
Copy after login

Related recommendations: php training

The above is the detailed content of How to delete specified elements in 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