Home >Backend Development >PHP Problem >How to remove subscripts from php array
In PHP, array is a very common data type. Arrays usually consist of a set of keys and corresponding values. The keys represent the serial numbers or names of the array elements, and the values represent the corresponding data. When using arrays, you often encounter situations where you need to remove the keys (subscripts) of array elements. This article will introduce how to remove the subscripts of array elements in PHP.
Method 1: Use the array_values() function
The array_values() function is one of the commonly used functions in PHP. It is used to obtain all the values in an array and return it as an index array ( The key value starts from 0 and increases). This function can help us quickly remove the subscripts of array elements. The code is as follows:
$array = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry'); $array = array_values($array); print_r($array);
The running results are as follows:
Array ( [0] => apple [1] => banana [2] => cherry )
In the code, first we define an associative array and give each The element has a key value set. Then use the array_values() function to convert the original array into an index array, so that all keys (subscripts) in the original array can be removed, and the keys of all elements in the returned array are numbers increasing from 0.
Method 2: Use the unset() function
Another common method is to use the unset() function in PHP. This function can be used to delete specified elements from an array. If you want to delete the keys (subscripts) of the entire array, you can loop through the array and use the unset() function to delete the keys of each element in turn. The code is as follows:
$array = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry'); foreach ($array as $key => $value) { unset($array[$key]); } print_r($array);
The running results are as follows:
Array ( [0] => apple [1] => banana [2] => cherry )
In the code, we first define an associative array. Then use a foreach loop to iterate through the array, and use the unset() function to delete the key (subscript) of each element in the array in turn. Finally, the array is output again and it is found that all elements in the array have become index arrays, and the subscripts increase from 0.
Method 3: Use the array_map() function
The array_map() function is a commonly used array processing function in PHP, which can perform specified operations on each element in the array. In this method, we can use the array_map() function to assign the key (subscript) of each element to empty. The code is as follows:
$array = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry'); $array = array_map(function($value){ return $value; }, $array); print_r($array);
The running results are as follows:
Array ( [a] => apple [b] => banana [c] => cherry )
In the code , we first define an associative array and then use the array_map() function to apply a callback function to each element in the array. In the callback function, we simply return the value of each element and do nothing with the elements. This results in a new array with the same number and order of elements as the original array, but with empty keys (subscripts) for all elements.
Summary
Through the above three methods, we can easily remove the subscripts (keys) of array elements in PHP and convert the array into an indexed array. The array_values() function is applicable to all array types, the unset() function is applicable to associative arrays, and the array_map() function is applicable to any array type. When using these methods, you need to choose the appropriate method according to the specific situation.
The above is the detailed content of How to remove subscripts from php array. For more information, please follow other related articles on the PHP Chinese website!