PHP array_values is an inbuilt Function in PHP. It returns all the values of an array. In this article, we will discuss the PHP array_values Function. Also, we will discuss a few examples.
The PHP array_values function expects only one mandatory parameter which is the original array for which the function will fetch the values.
The PHP array_values Function creates another array in which it stores the values of the original array and returns it.
The Function returns any nested array as it is and does not process it.
Let’s discuss a few examples to demonstrate the use of array_values Function in PHP.
Example 1: Basic Usage
<?php $testArray = array( 'apple' => 1, 'mango' => 24, 5 => 10, 'grapes' => array( 'black' => 1, 'green' => 2 ) ); $values = array_values($testArray); print_r($values); /* Array (  => 1  => 24  => 10  => Array ( [black] => 1 [green] => 2 ) ) */ ?>
In the above example, an array with mixed keys is passed to the array_values. You can observe the following things:
- The array_values Function assigns default numeric keys in the final array. It discards all the original keys of the array.
- Also, it returns the nested array as it is. The output array still contains the original keys of the inner array (grapes -> black/green).
In conclusion, we discussed the array_values Function. It is useful in retrieving values from an array. You can read more about it on the Official Documentation of PHP. You can also browse through other PHP Array Functions on Concatly.
Vishesh is currently working as a Senior Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.