The following small series will provide you with an implementation method to delete the specified elements in the array based on the key. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the following small series to bring you an article on how to delete the elements specified in the array based on the key. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.
The element in the php array can exist as a key-value pair ('key' => 'value'). sometimes we need to delete an element specified in the array based on the key.
function bykey_reitem($arr, $key){ if(!array_key_exists($key, $arr)){ return $arr; } $keys = array_keys($arr); $index = array_search($key, $keys); if($index !== FALSE){ array_splice($arr, $index, 1); } return $arr; } $data = array('name'=>'apple','age'=>12,'address'=>'ChinaGuangZhou'); $result = array_remove($data, 'name'); var_dump($result);
Function usage instructions:
1. 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.
Example 1
"Dog","b"=>"Cat","c"=>"Horse"); echo array_search("Dog",$a); ?>
2. array_splice ()
Definition and usage
The array_splice () function is similar to the array_slice () function. it selects a series of elements in the array but does not return them. Instead, it deletes them and replaces them with other values.
If the fourth parameter is provided, the selected elements are replaced by the array specified by the fourth parameter. The final generated array will be returned.
Syntax
Array_splice (array, offset, length, array)
Parameter description
ArrayRequired. Specified array.
OffsetRequired. Value. If the offset value is positive, it is removed from the offset specified by this value in the input array. If offset is negative, it is removed from the offset specified by the reciprocal value at the end of the input array.
LengthOptional. Value. If this parameter is omitted, all parts from the offset to the end of the array are removed. If length is specified and it is a positive value, so many elements are removed. If length is specified and it is a negative value, all the elements from the offset to the reciprocal length at the end of the array are removed.
ArrayThe removed element is replaced by the element in the array. If no value is removed, the elements in this array are inserted to the specified position.
Tips and comments
Tip: If the function does not delete any element (length = 0), the replacement array is inserted from the start parameter. (See Example 3)
Note: do not retain the replacement keys in the array.
Instance
"Dog", 1 => "Cat", 2 => "Horse", 3 => "Bird"); $ a2 = array (0 => "Tiger ", 1 => "Lion"); array_splice ($ a1, $ a2); print_r ($ a1); // output: array ([0] => Tiger [1] => Lion [2] => Horse [3] => Bird)?> // Same as example 1, but the returned array is output:
"Dog", 1 => "Cat", 2 => "Horse", 3 => "Bird"); $ a2 = array (0 => "Tiger ", 1 => "Lion"); print_r (array_splice ($ a1, 0, 2, $ a2);?> // Output: Array ([0] => Dog [1] => Cat) // set the length parameter to 0:
"Dog", 1 => "Cat"); $ a2 = array (0 => "Tiger", 1 => "Lion"); array_splice ($ a1, 1, 0, $ a2); print_r ($ a1);?> // Output: Array ([0] => Dog [1] => Tiger [2] => Lion [3] => Cat)
The above is the content of PHP to delete the specified element implementation method in the array based on the key. For more information, see PHP Chinese network (www.php1.cn )!