In PHP development, we often encounter to delete the specified content in the array, but the array is a special variable we can not directly use replace replacement, need some way to operate, let me introduce you to the Operation method.
If we know the name of the array element, this is good.
Key name to delete the specified array element in the array
The code is as follows |
Copy Code |
$barray = Array (' A ' =>1, ' B ' =>2, ' WOD ' =>3, ' C ' =>4, ' abc ' =>5); $del = ' B '; Unset ($barray [$del]);//result is Array ( [A] = 1 [WOD] = 3 [C] = 4 [ABC] = 5 )
|
If there are multiple arrays to be deleted at the same time, the above approach is not resolved, we can use the Array_diff function to manipulate
Cases
The code is as follows |
Copy Code |
$a 1=array ("Cat", "Dog", "Horse", ' DFF ', ' dfdf ', ' www '); $a 2=array ("DFF", "Horse", "Dog"); $a 1 = Array_diff ($a 1, $a 2); Sort ($a 1); Print_r ($a 1); Array ( [0] = Cat [1] = DFDF [2] = = www ) |
Example 2
The Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. Returns false if not found
The code is as follows |
Copy Code |
$array = Array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ');
$del _value = 3; Unset ($array [Array_search ($del _value, $array)]);//use unset to delete this element Print_r ($array); Output Array (' 1 ', ' 2 ', ' 4 ', ' 5 '); |
Cases
Array_filter ()
Call mode: Array_filter ($array)
Parameter description: $array is the object of the operation, we will delete the empty element in it
Instance:
The code is as follows |
Copy Code |
$array = (' a ' + = ' abc ', ' B ' + = ' bcd ', ' c ' = ' CDE ', ' d ' and ' Def ', ' e ' = ' = ') '; Array_filter ($array); echo " "; Print_r ($array); ?>Results: Array ( [A] = ABC [B] = BCD [C] = CDE [d] = def ) |
None of the above-mentioned methods will be rebuilt, so let me show you a way to delete an array element and rebuild the array index.
The code is as follows |
Copy Code |
Function Array_remove (& $arr, $offset) { Array_splice ($arr, $offset, 1); } $a = Array (' A ', ' B ', ' C ', ' d '); Array_remove ($a, 2); Print_r ($a); |
http://www.bkjia.com/PHPjc/628853.html www.bkjia.com true http://www.bkjia.com/PHPjc/628853.html techarticle in PHP development, we often encounter to delete the specified content in the array, but the array is a special variable we can not directly replace the replacement, need some way to operate, the following ...