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

How to remove a value from an array in php

王林
王林Original
2020-08-05 14:08:024108browse

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)

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)

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

Output result:

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

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!

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