PHP array_values() Function

What is PHP array_values() Function?

If you want to create an array taking values from another array discarding its keys, use array_values() function. The keys of the new array become numeric that start at 0 and increase by 1. In the new array, the order of the values remains same as the original array. Though it may seem not much useful, but, this function is very useful and helpful in many situations.

Syntax:

array_values(array)

Parameters:

The Function has 1 parameter which is mandatory –

array (Required): The array whose values you want to take to create the final array.

Return Values:

The function returns an indexed array whose index starts from 0 and increases by 1. Check example 1.

Examples:

Example 1: Creating new array-

<pre>
<?php
$arr = ["First Day" => "Sunday", "Second Day" => "Monday", "Third Day" => "Tuesday", "Fourth Day" => "Wednesday", "Fifth Day" => "Thursday", "Sixth Day" => "Friday",  "Seventh Day" => "Saturday"];
print_r(array_values($arr));
?>
</pre>

Output:

Array
(
    [0] => Sunday
    [1] => Monday
    [2] => Tuesday
    [3] => Wednesday
    [4] => Thursday
    [5] => Friday
    [6] => Saturday
)

Explanation:

The array_values() function creates an indexed array from this associative array.

Example 2: Serialize keys of an array-

<pre>
<?php
$arr = [0 => "Sunday", 2 => "Monday", 11 => "Tuesday", 1 => "Wednesday", 3 => "Thursday", 4 => "Friday",  7 => "Saturday"];
print_r(array_values($arr));
?>
</pre>

Output:

Array
(
    [0] => Sunday
    [1] => Monday
    [2] => Tuesday
    [3] => Wednesday
    [4] => Thursday
    [5] => Friday
    [6] => Saturday
)

Explanation:

The function creates a new array from this random key array. The keys of the new array start at 0 and increases by 1.

Example 3: Separating numeric keys from string keys-

<pre>
<?php
$arr = ["Sunday Sale: ", 99, "Monday Sale: ", 145, "Tuesday Sale: ", 150, "Wednesday Sale: ", 139, "Thursday Sale: ", 135, "Friday Sale: ", 150, "Saturday Sale: ", 101];
$arr = array_filter($arr, "is_numeric");
print_r(array_values($arr));
?>
</pre>

Output:

Array
(
    [0] => 99
    [1] => 145
    [2] => 150
    [3] => 139
    [4] => 135
    [5] => 150
    [6] => 101
)

Explanation:

Line 4: The array_filter() function separates numeric values from the array.

Line 5: The array_values() function serializes the values of the array.

Example 4: Removing duplicate values-

<pre>
<?php
$arr = [0 => "PHP",  2 => "Pyhton", 7 => "C", 8 => "C#", 9 => "C", 10 => "PHP"];
$newArr = array_unique($arr);
print_r(array_values($newArr));
?>
</pre>

Output:

Array
(
    [0] => PHP
    [1] => Pyhton
    [2] => C
    [3] => C#
)

Explanation:

Line 4: the array_unique() function removes duplicate values from the array. but the keys are not serialized in the array.

Line 5: The array_values() function serialize the values of the array.

Example 5: Creating a copy of an array-

<pre>
<?php
$arr = ["Sunday",  "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
$newArr = array_values($arr);
$newArr[] = "Saturday";
print_r($newArr);
?>
</pre>

Output:

Array
(
    [0] => Sunday
    [1] => Monday
    [2] => Tuesday
    [3] => Wednesday
    [4] => Thursday
    [5] => Friday
    [6] => Saturday
)

Explanation:

Line 4: The array_filter() function makes a copy from the $arr array.

Line 5: We adds a new element “Saturday” in this new array. And it doesn’t affect the original array $arr.

Example 6: Creating a multidimensional array-

<pre>
<?php
$arr = array(
    "Client" => array(4 => "HTML", 3 => "CSS"),
    "Server" => array("One" => "PHP", "Two" => "MySQL"),
    "OS" => "Linux"
);
$newArr = array_values($arr);
print_r($newArr);
?>
</pre>

Output:

Array
(
    [0] => Array
        (
            [4] => HTML
            [3] => CSS
        )
    [1] => Array
        (
            [One] => PHP
            [Two] => MySQL
        )
    [2] => Linux
)

Explanation:

Line 8: The array_filter() function makes a copy from the $arr array. In the new array, the keys of the first level are re-indexed, but the keys of the nested level arrays are not affected.

Practical Usages of array_values() Function:

Among many practical usages few include-

  • Suppose you have an indexed array whose numeric keys are not serialized. You want to serialize the keys without removing any values and keep the values in the same order. to solve it, use the array_values() function to achieve the result. Check example 2.
  • In some situation you may need to remove keys and convert an associative array to an indexed array. In this case, array_values() function can do it easily. Check example 1.
  • You may need to separate numeric values from string values in an associative array and re-index. To make it happen, first use array_filter() function to separate numeric values and create a new array. Then, use array_values() function to re-index the array. Check example 3.
  • Suppose you want to remove duplicate values from an array and then re-index it. To accomplish it, at first apply array_unique() function to remove duplicate values from the array and then apply array_values() function to re-index the array. Check example 4.
  • You may want to work on an array but you don’t want to make any change on this. In this case, you can make a copy of this array easily with array_values() function and perform your further operations on it. Check example 5.

Notes on array_values() Function:

  • You can apply array_values() function on multidimensional array. But it works partially. The function takes all the elements and it re-indexes the first level, but it can’t re-index the keys of the nested level. Check example 6.

Cautions:

  • As the array_values() function creates a new array discarding old array keys, you can’t use those keys in your new array.
  • Creating an array with this function from a large array may take lots of memory. So, take this in your consideration in this situation and take possible alternate solutions.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP array_values() Function

array_values() is a very useful function that helps you effortlessly create an array from taking values of another array. Harness the power of this built-in array function with other function to accomplish your different tasks.

Reference:

https://www.php.net/manual/en/function.array-values.php