Tutorialspoint.dev

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:

// 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));
?>

Output:

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

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



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter