Home >Backend Development >PHP Tutorial >The array_values() function in PHP gets the values ​​in the array

The array_values() function in PHP gets the values ​​in the array

PHPz
PHPzOriginal
2023-11-18 17:20:111673browse

The array_values() function in PHP gets the values ​​in the array

The array_values() function in PHP gets the values ​​in the array

In PHP, array is a very commonly used and important data type. In the actual development process, we often need to operate the values ​​​​in the array. Among them, the array_values() function is a very useful function, which can be used to get all the values ​​in the array and return a new index array.

The syntax of the array_values() function is as follows:
array_values(array $array): array

This function requires an array as a parameter and returns a new index array in which the All values ​​in the original array. The following is the specific description of this function:

  1. Parameter $array: the original array of values ​​that needs to be obtained.
  2. Return value: a new index array containing all the values ​​in the original array.

Next, we use specific code examples to demonstrate the use of the array_values() function:

<?php
// 定义一个关联数组
$student = [
  "name" => "张三",
  "age" => 18,
  "gender" => "男"
];

// 使用array_values()函数获取数组中的值
$values = array_values($student);

// 打印输出新的索引数组
print_r($values);
?>

The output result of the above code is:

Array
(
    [0] => 张三
    [1] => 18
    [2] => 男
)

Passed From the above code example, we can see that the array_values() function extracts the values ​​​​in the original array $student and stores them in a new index array $values. The order of the elements in the new index array $values ​​is consistent with the order of the values ​​in the original array $student.

It should be noted that the array_values() function will only obtain the values ​​​​in the original array, but will not retain the keys in the original array. Therefore, the keys in the returned new index array are all integers 0, 1, 2, 3...increasing in sequence.

To sum up, the array_values() function is a very practical function. It can easily obtain the values ​​in the array and return a new index array for further processing and use by developers. Whether it is array operations or data processing, the functions provided by the array_values() function can be more efficient and convenient.

The above is the detailed content of The array_values() function in PHP gets the values ​​in the array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn