Home > Backend Development > PHP Problem > How to remove a value from an array in php

How to remove a value from an array in php

王林
Release: 2023-03-03 20:28:01
Original
4149 people have browsed it

php method to remove a value from an array: first use the array_search() function to search for the key value that needs to be removed in the array; then use the array_splice() function to remove the selected element from the array. Yes, this function will also return an array of removed elements.

How to remove a value from an array in php

array_search() function searches for a key value in the array and returns the corresponding key name.

(Recommended tutorial: php graphic tutorial)

Syntax:

array_search(value,array,strict)
Copy after login

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

Grammar:

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

(Video tutorial recommendation: php video tutorial)

Code implementation:

<?php
$tmp = &#39;324&#39;;
$arr = array(
&#39;0&#39; => &#39;321&#39;,
&#39;1&#39; => &#39;322&#39;,
&#39;2&#39; => &#39;323&#39;,
&#39;3&#39; => &#39;324&#39;,
&#39;4&#39; => &#39;325&#39;,
&#39;5&#39; => &#39;326&#39;,
);

$key=array_search($tmp ,$arr);
array_splice($arr,$key,1);
var_dump($arr);
?>
Copy after login

Output result:

Array
(
 [0] => 321
 [1] => 322
 [2] => 323
 [3] => 325
 [4] => 326
)
Copy after login

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