PHP Function Reference

PHP array_values() Function



The PHP array_values() function returns all the values from the array and indexes the array numerically.

Syntax

array_values(array)

Parameters

array Required. Specify the input array.

Return Value

Returns an indexed array of values.

Exceptions

NA.

Example: using with indexed array

The example below shows the usage of array_values() function when used with indexed array.

<?php
$Arr = array(10, 20, 30, 20, 40, 20);

//displaying all values of the array
$val_Arr = array_values($Arr);
print_r($val_Arr);
?>

The output of the above code will be:

Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 20
    [4] => 40
    [5] => 20
)

Example: using with associative array

Consider one more example where array_values() function is used with an associative array.

<?php
$Arr = array(101=>"Red", 
             102=>"Blue", 
             103=>"Green",
             104=>"White",
             105=>"Black",
             106=>"Green");

//displaying all values of the array
$val_Arr = array_values($Arr);
print_r($val_Arr);
?>

The output of the above code will be:

Array
(
    [0] => Red
    [1] => Blue
    [2] => Green
    [3] => White
    [4] => Black
    [5] => Green
)

❮ PHP Array Reference