Home > Article > Backend Development > How to get multiple values from an array in php
In PHP, we often need to take out multiple values from an array for processing, such as performing certain calculations, statistics, etc. So, how to remove multiple values from the array? Here, we will introduce several methods to retrieve multiple values from an array in PHP.
The list() function is a very convenient and concise way to take out multiple values from an array. The syntax of this function is as follows:
list($var1, $var2, …) = $array;
Among them, $var1, $var2 are the variable names of the element values to be taken out of the array, and $array is the array to be operated on.
For example, we have an array $colors containing 3 elements, whose element values are "red", "green", and "blue" respectively. Then we can use the list() function to take out this array The first two element values of:
$colors = array("red", "green", "blue"); list($color1, $color2) = $colors; echo "$color1, $color2"; // 输出"red, green"
Array destructuring (array destructuring) is a new feature introduced in PHP 7.1, which can be more convenient Remove multiple values from an array. Compared with using the list() function, array destructuring is more flexible, and the retrieved value can be assigned to any variable.
The syntax of array destructuring is as follows:
[$var1, $var2, …] = $array;
Among them, $var1, $var2 are the variable names of the element values to be taken out of the array, and $array is the array to be operated on.
The following is an example, we use array destructuring to remove the first two element values in the array $colors:
$colors = array("red", "green", "blue"); [$color1, $color2] = $colors; echo "$color1, $color2"; // 输出"red, green"
array_slice($array, $offset, $length);Among them, $array is the array to be operated, $offset is the position where the elements are to be obtained, and $length is the number of elements to be obtained. For example, we have an array $numbers containing 5 elements. Its element values are 1, 2, 3, 4, 5 respectively. Then we can use the array_slice() function to get the first three elements of the array. Element values:
$numbers = array(1, 2, 3, 4, 5); $firstThreeNumbers = array_slice($numbers, 0, 3); print_r($firstThreeNumbers); // 输出Array([0] => 1,[1] => 2,[2] => 3)It should be noted that the array_slice() function will return a new array, and the original array will not be affected.
$person = array( "name" => "Tom", "age" => 30, "gender" => "male", ); foreach ($person as $key => $value) { echo "$key: $value\r\n"; }The above code will output the following results in sequence:
name: Tom age: 30 gender: maleConclusionThere are many ways to retrieve multiple values from an array, among which the list() function and array destructuring are the most commonly used. If we only need to take out a few consecutive elements in the array, we can use the array_slice() function. If you want to iterate through the entire array, you can use a foreach loop. In practical applications, you can choose different methods to retrieve multiple values from an array according to specific circumstances.
The above is the detailed content of How to get multiple values from an array in php. For more information, please follow other related articles on the PHP Chinese website!