$v){empty array name[]=$k;}". 3. Use array_splice(), the syntax is "array_splice(array, delete position, delete length)". 4. Use array_slice(), the syntax is "array_slice (array, interception position, length)"."/> $v){empty array name[]=$k;}". 3. Use array_splice(), the syntax is "array_splice(array, delete position, delete length)". 4. Use array_slice(), the syntax is "array_slice (array, interception position, length)".">
Home > Article > Backend Development > How to remove array value (key value) in php
4 methods: 1. Use array_keys(), the syntax is "array_keys (array)". 2. Use foreach and an empty array, the syntax is "foreach(original array as $k=>$v){empty array name[]=$k;}". 3. Use array_splice(), the syntax is "array_splice(array, delete position, delete length)". 4. Use array_slice(), the syntax is "array_slice (array, interception position, length)".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
php removes array value Multiple methods of (key values)
Method 1: Use the array_keys() function to remove all values (key values)
array_key() The function can retrieve some or all keys in the array.
Use the array_key() function to remove all key values from the array and return a new array containing all key names.
Usage syntax:
array_keys($array)
Example:
<?php header("Content-type:text/html;charset=utf-8"); $arr=array("Name"=>"Peter","Age"=>"41","Country"=>"USA"); var_dump($arr); $keys=array_keys($arr); var_dump($keys); ?>
##Method 2: Use a foreach loop and an empty array to remove all values (Key value)
<?php $arr1=array("aaa"=>11,"bbb"=>22,"ccc"=>33); var_dump($arr1); $arr2=array(); foreach($arr1 as $k=>$v){ $arr2[]=$k; } var_dump($arr2); ?>
Method 3: Use the array_splice() function to remove the array value (Key value)
When the array_splice() function deletes a part of the elements of the array, it will form these deleted elements into a new array Usage syntax:array_splice(array1,start,length,array2)
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.
The above is the detailed content of How to remove array value (key value) in php. For more information, please follow other related articles on the PHP Chinese website!