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.
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 = '324'; $arr = array( '0' => '321', '1' => '322', '2' => '323', '3' => '324', '4' => '325', '5' => '326', ); $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!