&$v){if($v==="string"){unset($arr[$k]); }}"."/> &$v){if($v==="string"){unset($arr[$k]); }}".">

Home  >  Article  >  Backend Development  >  How to remove a certain string from a php array

How to remove a certain string from a php array

青灯夜游
青灯夜游Original
2022-08-26 20:22:421768browse

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]) ;}}".

How to remove a certain string from a php array

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:
  • true
  • false - default
If set to true, the type of the given value in the array is checked, the number 5 and the string 5 are different (see Example 2).
<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(10,"12",12,25,24);
echo "原数组:";
var_dump($arr); 
$key=array_search("12",$arr,true);
echo "指定字符串&#39;12&#39;的键名为:".$key;

?>

How to remove a certain string from a php array

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

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(10,"12",12,25,24);
echo "原数组:";
var_dump($arr); 
$key=array_search("12",$arr,true);
echo "指定字符串&#39;12&#39;的键名为:".$key;


array_splice($arr,$key,1);
echo "<br><br>删除指定字符串&#39;12&#39;后:";
var_dump($arr); 
?>

How to remove a certain string from a php array

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:

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(10,"25",12,25,"24");
echo "原数组:";
var_dump($arr); 
foreach ($arr as $key => &$value){
	if($value==="25"){
		unset($arr[$key]);
	}
}
echo "<br>删除指定字符串&#39;25&#39;后:";
var_dump($arr); 
?>

How to remove a certain string from a php array

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); // 最后取消掉引用

How to remove a certain string from a php array

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!

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