Php array operation-delete a specified array element. This article condenses the usage of various php functions to remove array elements by specified element values. if you need them, refer to them. The code for removing array elements based on specified element values is as follows: This article describes the usage of various php functions to remove array elements based on specified element values. For more information, see.
Removes array elements based on specified element values
The code is as follows: |
|
$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse "); Print_r ($ ); Unset ($ a [array_search ("Cat", $ a)]); // Array_search ("Cat", $ a) returns the key name based on the element value. Keep indexes after removal Print_r ($ ); ?> |
The usage of array_search is described below.
Display result
The code is as follows: |
|
Before removal: Array ( [A] => Dog [B] => Cat [C] => Horse ) After removal: Array ( [A] => Dog [C] => Horse ) |
Array_search () definition and usage
The array_search () function is the same as the in_array () function. you can find a key value in the array. If this value is found, the key name of the matching element is returned. If not found, false is returned.
Before PHP 4.2.0, the function returns null instead of false in case of failure.
If the third parameter strict is set to true, the key name of the corresponding element is returned only when the data type and value are consistent.
Syntax
Array_search (value, array, strict) parameter description
Value is required. Specifies the value to be searched in the array.
Array is required. The searched array.
Strict is optional. Possible values:
True
False-default
If the value is set to true, the type of the given value is also checked in the array. (See example 2)
Example 1
The code is as follows: |
|
$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse "); Echo array_search ("Dog", $ ); ?> Output: |
A
Bytes. The code for removing array elements based on specified element values is as follows...