What is the array_values method in PHP?

The array_values method in PHP returns all of the values of the arraya data structure that contains a group of elements.

Syntax

array_values($array): array

Return type

The array_values method returns an array.

Code

Example 1

<?php
$age_array = array("Rahum" => 12, "Ram" => 22);
print_r( array_values( $age_array));
?>

Explanation

In the code above, we create an array with the name age_array and call the array_values method.

The array_values method returns a new array with all values of age_array. The indexing of the returned array will be based on default numeric indexing.

Example 2

<?php
$multi_array = array(
'one' => 1,
'two' => array('one' => 2, 'two' => 22)
);
print_r( array_values( $multi_array));
?>

Explanation

In the above code, we create a multidimensional array with multi_array and call the array_values method. The array_values method will return a new array with all values of multi_array.

Free Resources