How to remove array elements in php

王林
Release: 2023-03-03 18:58:02
Original
2151 people have browsed it

php method to remove array elements: You can use the array_splice() function to achieve this. The array_splice() function removes the selected element from the array and replaces it with the new element, returning the array containing the extracted elements.

How to remove array elements in php

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

(Recommended tutorial: php graphic tutorial)

Syntax:

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);
print_r($array);
?>
Copy after login

Output Result:

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

(Video tutorial recommendation: php video tutorial)

Example 2:

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"purple","b"=>"orange");
print_r(array_splice($a1,0,2,$a2));
?>
Copy after login

Output result:

How to remove array elements in php

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