PHP function introduction—array_values(): Returns an array of all elements in the array
In PHP development, array is a very commonly used data structure. PHP provides a wealth of array processing functions, allowing us to operate and process arrays more conveniently. This article will introduce a very practical array function-array_values(), which can return a new array containing all elements in the array.
The array_values() function is to return a new array of all elements in the array. The key names of the new array will be incremented from 0 again. The usage of this function is very simple, you only need to pass in an array as a parameter. The following is a specific code example:
<?php $array = array("apple", "banana", "cherry"); $newArray = array_values($array); print_r($newArray); ?>
Execute the above code, the following results will be output:
Array ( [0] => apple [1] => banana [2] => cherry )
As you can see, the key names in the new array returned by the array_values() function start from 0 Start incrementing, and the corresponding value remains consistent with the original array. This is very useful in certain scenarios, such as when we need to re-index an array, or when we need to convert an associative array into an indexed array, etc.
In addition to ordinary index arrays, the array_values() function can also handle associative arrays. For associative arrays, it retains the key names of the original array and returns an index array increasing from 0. The following is a sample code for an associative array:
<?php $student = array( "name" => "张三", "age" => 18, "score" => 95 ); $newArray = array_values($student); print_r($newArray); ?>
Execute the above code, the following results will be output:
Array ( [0] => 张三 [1] => 18 [2] => 95 )
As you can see, the array_values() function converts the associative array into an index array, and The values of the original array are retained. This is very convenient in certain situations, such as looping through associative arrays or other operations.
It should be noted that the new array returned by the array_values() function is not a reference to the original array, but a new array. This means that modifications to the new returned array will not affect the values of the original array. For example:
<?php $array = array("apple", "banana", "cherry"); $newArray = array_values($array); $newArray[0] = "orange"; print_r($newArray); // 输出:Array ( [0] => orange [1] => banana [2] => cherry ) print_r($array); // 输出:Array ( [0] => apple [1] => banana [2] => cherry ) ?>
As you can see from the above code, although the first element of the new array has been modified, the original array has not changed in any way.
To sum up, the array_values() function is a very practical array processing function that can help us achieve the desired results when operating and processing arrays. It can return a new array containing all elements in the array, and can handle ordinary indexed arrays and associative arrays. In actual PHP development, we can reasonably use this function according to specific needs and scenarios to improve the readability and maintainability of the code.
The above is the detailed content of PHP function introduction—array_values(): returns an array of all elements in the array. For more information, please follow other related articles on the PHP Chinese website!