This article condenses the usage of various php functions to remove array elements based on the specified element values and remove array elements based on the specified element values. the code is as follows: & lt ;? Elements with a value of & quot; Cat & quot; removed from php $ Comment R
This article condenses the usage of various php functions to remove array elements based on the specified element values and remove array elements based on the 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 following code describes how to use array_search:
- // Before removal:
- Array
- (
- [A] => Dog
- [B] => Cat
- [C] => Horse
- )
- // After removal:
- Array
- (
- [A] => Dog
- [C] => Horse
- )
Array_search () definition and usage
Like the in_array () function, array_search () finds a key value in the array. if this value is found, the key name of the matching element is returned. if this value is 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)
Parameters:Value is required, which specifies the value to be searched in the array.
Parameters:Array is required. The searched array.
Parameters: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.
The sample code is as follows:
-
- $ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
- Echo array_search ("Dog", $ );
- ?> // Output: