Latest Code Tutorials

PHP Array Values Example | PHP array_values() Function Tutorial


PHP array_values() is an inbuilt function that returns all the values of an array and not the keys. The array_values() function returns the array containing all the values of an array. The returned array will have the numeric keys, starting at 0 and increase by 1. The array_keys() inbuilt function is used to get an array of values from another array that may contain key-value pairs or just values.

PHP Array Values Example

The PHP array_keys() function creates another array where it stores all the values and by default assigns numerical keys to the values. See the below syntax.


An array is a required parameter, and it specifies the array.

The array_values() function takes only one parameter as an array which is mandatory and refers to the original input array, from which values need to be fetched.

See the below code example.


// app.php

$data = ['a' => 'Apple', 
        'm' => 'Microsoft', 
        'b' => 'Amazon', 
        'c' => 'Alphabet', 
        'f' => 'Facebook'];

See the output.

PHP Array Values Example

If you want to fetch all the keys from an array, then you can check out the array_keys() function in PHP.

Remember, PHP array_values() method will ignore your beautiful numeric indexes; it will renumber them according to the ‘foreach’ ordering.

See the below code example.


// app.php

$data = array(
  19 => 21,
  21 => 19,
  46 => 21,
$data[29] = 21;
echo 'After array_values() function';  

See the below output.

PHP array_values() Function Tutorial

Just a warning that re-indexing an array by array_values() may cause you to reach the memory limit unexpectedly.

The array_keys() and array_values() are very closely related functions. The first function returns an array of all the keys in an array, and the second returns an array of all the values in the array.

For example, consider you had an array with user IDs as keys and usernames as values, you could use array_keys() to generate an array where the values were the keys.

Conclusively, PHP Array Values Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.