📜  php array_values - PHP (1)

📅  最后修改于: 2023-12-03 14:45:10.475000             🧑  作者: Mango

PHP array_values()

The array_values() function in PHP is used to retrieve all the values from an array and create a new indexed array out of it. This function returns a numerically indexed array where the keys are numbered starting from 0.

Syntax

The syntax for using the array_values() function is as follows:

array_values(array $array): array

Here, the array parameter is the input array. It is a mandatory parameter and needs to be an array. The function will return a new array with the values of the input array.

Example

Let's look at an example of how to use the array_values() function in PHP:

// Declare an associative array
$fruits = array(
    "a" => "apple",
    "b" => "banana",
    "c" => "cherry"
);

// Get the values of the array using array_values()
$values = array_values($fruits);

// Display the output
print_r($values);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)

Using the array_values() function, we have converted an associative array $fruits into a new numerically indexed array $values. The keys have been renumbered starting from 0.

Conclusion

The array_values() function is a useful function in PHP. It allows you to retrieve all the values from an array and create a new indexed array out of it. This function is often used with associative arrays to convert them into numerically indexed arrays.