PHP | array_values() Function

This inbuilt function in PHP is used to get an array of values from another array that may contain key-value pairs or just values. The function creates another array where it stores all the values and by default assigns numerical keys to the values.

Syntax:

array array_values($array)

Parameters: This function takes only one parameter $array which is mandatory and refers to the original input array, from which values needs to be fetched.



Return Value: This function returns an array with the fetched values, indexed with the numerical keys.

Examples:

Input : $array = ("ram"=>25, "krishna"=>10, "aakash"=>20, "gaurav")
Output :
Array
(
    [0] => 25
    [1] => 10
    [2] => 20
    [3] => gaurav
)

Input : $array = ("ram", "krishna", "aakash", "gaurav")
Output :
Array
(
    [0] => ram
    [1] => krishna
    [2] => aakash
    [3] => gaurav
)

Below program illustrates the array_values() function in PHP:

filter_none

edit
close

play_arrow

link
brightness_4
code

// PHP function to illustrate the use of array_values()
function Return_Values($array)
{
    return (array_values($array));
}
  
$array = array("ram"=>25, "krishna"=>10, "aakash"=>20, "gaurav");
print_r(Return_Values($array));
?>

chevron_right


Output:

Array
(
    [0] => 25
    [1] => 10
    [2] => 20
    [3] => gaurav
)

Reference:
http://php.net/manual/en/function.array-values.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.