Using PHP’s array_values() Function to Reindex Arrays

This article explains PHP’s array_values() function, showing how it returns a new array with reindexed numeric keys, works with both indexed and associative arrays, provides code examples, and highlights that the returned array is a copy, not a reference to the original.

php Courses
php Courses
php Courses
Using PHP’s array_values() Function to Reindex Arrays

In PHP development, arrays are a fundamental data structure, and the language provides many built‑in functions to manipulate them. This article focuses on the array_values() function, which returns a new array containing all the values of the original array with numeric keys reindexed from zero.

The function is straightforward: pass any array as the argument, and it returns a new array whose keys start at 0 and increment sequentially, while preserving the original values.

Example with a simple indexed array:

<?php
$array = array("apple", "banana", "cherry");
$newArray = array_values($array);
print_r($newArray);
?>

Running this code outputs:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)

This demonstrates that array_values() returns a new array whose keys are reset to 0, 1, 2, matching the original values, which is useful for re‑indexing or converting associative arrays to indexed ones.

Example with an associative array:

<?php
$student = array(
    "name" => "张三",
    "age"  => 18,
    "score"=> 95
);
$newArray = array_values($student);
print_r($newArray);
?>

The output is:

Array
(
    [0] => 张三
    [1] => 18
    [2] => 95
)

Here, array_values() converts the associative array into an indexed array, discarding the original keys but keeping the values in the same order.

It is important to note that the array returned by array_values() is a copy, not a reference to the original array. Modifying the new array does not affect the source array:

<?php
$array = array("apple", "banana", "cherry");
$newArray = array_values($array);
$newArray[0] = "orange";
print_r($newArray); // Array ( [0] => orange [1] => banana [2] => cherry )
print_r($array);    // Array ( [0] => apple  [1] => banana [2] => cherry )
?>

This confirms that changes to the returned array are isolated from the original.

In summary, array_values() is a practical function for obtaining a re‑indexed list of values from both indexed and associative arrays, improving code readability and simplifying further array operations in PHP backend development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendArrayTutorialarray_values
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.