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

王林
王林Original
2020-10-29 17:19:253492browse

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)

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);
?>

Output result:

Array(
    [0] => a    
    [1] => c
)

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!

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