Home>Article>Backend Development> How to remove a certain string from a php array
Two methods: 1. Use array_search() and array_splice() to delete, the syntax is "array_splice($arr,array_search("string",$arr,true),1);". 2. Use the foreach statement and unset() to delete, the syntax "foreach($arr as $k=>&$v){if($v==="string"){unset($arr[$k]) ;}}".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
Method 1: Use array_search() array_splice()
Step 1: Use the array_search() function to search for the specified string in the array and return the corresponding key name
array_search(value,array,strict)
Parameters | Description |
---|---|
value | Required. Specifies the key value to search for in the array. |
array | Required. Specifies the array to be searched. |
strict | Optional. If this parameter is set to TRUE, the function searches the array for elements of the same data type and value. Possible values:
|
Step 2: Use the array_splice() function to delete the element based on the obtained key name
array_splice() function removes a selected element from an array (or replaces it with a new element).
array_splice(array1,start,length,array2)
Parameters | Description |
---|---|
array1 | Required . Specifies an array. |
start | Required. numerical value. Specifies the starting position of deleted elements. 0 = first element. If the value is set to a positive number, removal begins at the offset specified by the value in the array. If the value is set to a negative number, removal begins at the offset specified by the value from the end of the array. -2 means start from the second to last element of the array. |
length | Optional. numerical value. Specifies the number of elements to be removed, which is also the length of the returned array. If this value is set to a positive number, remove this number of elements. If this value is set to a negative number, all elements from start to length inverse of the end of the array are removed. If this value is not set, all elements from the position set by the start parameter to the end of the array are removed. |
array2 | Optional. Specifies the array with the elements to be inserted into the original array. If there is only one element, it can be set to a string and does not need to be set to an array. |
Description: This function will change the original array
删除指定字符串'12'后:"; var_dump($arr); ?>
Method 2: Use foreach Statement unset() function
Step 1: Use the foreach statement to traverse the array through a reference loop
foreach ($array as $key => &$value){ //循环体语句块; }
Traverse the given $array array , in each loop, the value of the current array will be assigned to $value, and the key name will be assigned to $key.
Generally, when using the foreach statement to traverse an array, it operates on the backup of the array and generally does not affect the array itself.
If you want to change the array through a loop, you can use a reference loop (add & before $value, so that the foreach statement will assign a value by reference instead of copying a value), then operate the array in the loop body, just will affect the array itself.
Step 2: In the loop body, use the "===" operator to find the specified string, and use the unset() function to delete the element
if($value==="指定字符串"){ unset($arr[$key]); }
Complete sample code:
&$value){ if($value==="25"){ unset($arr[$key]); } } echo "
删除指定字符串'25'后:"; var_dump($arr); ?>
It can be seen that there is an & before the last element. That is because the $value reference of the last element of the array is after the foreach loop. Will remain. We need to use unset() to destroy it.
unset($value); // 最后取消掉引用
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove a certain string from a php array. For more information, please follow other related articles on the PHP Chinese website!