Master PHP’s array_values(): Reindex Arrays Easily
This article explains how PHP's array_values() function reindexes both indexed and associative arrays, provides clear code examples, demonstrates that it returns a new array without affecting the original, and highlights practical scenarios where reindexing is useful.
In PHP development, arrays are a commonly used data structure. PHP provides a rich set of array handling functions that make it easier to operate on arrays. This article introduces a very useful array function array_values(), which returns a new array containing all elements of the original array.
The array_values() function returns a new array of all elements, with keys reindexed starting from 0. Its usage is simple: just pass an array as the argument. Below is a concrete code example:
<?php
$array = array("apple", "banana", "cherry");
$newArray = array_values($array);
print_r($newArray);
?>Running the above code outputs:
Array
(
[0] => apple
[1] => banana
[2] => cherry
)We can see that array_values() returns a new array whose keys start from 0 while the values remain the same. This is useful when you need to reindex an array or convert an associative array to an indexed array.
Besides ordinary indexed arrays, array_values() can also handle associative arrays. For associative arrays, it discards the original keys and returns an indexed array starting from 0. Example code:
<?php
$student = array(
"name" => "张三",
"age" => 18,
"score" => 95
);
$newArray = array_values($student);
print_r($newArray);
?>Running this code outputs:
Array
(
[0] => 张三
[1] => 18
[2] => 95
)We can see that array_values() converts the associative array into an indexed array while preserving the original values. This is convenient when you need to iterate over an associative array or perform other operations.
Note that the array returned by array_values() is not a reference to the original array but a new array. Modifying the new array does not affect the original. For example:
<?php
$array = array("apple", "banana", "cherry");
$newArray = array_values($array);
$newArray[0] = "orange";
print_r($newArray); // Output: Array ( [0] => orange [1] => banana [2] => cherry )
print_r($array); // Output: Array ( [0] => apple [1] => banana [2] => cherry )
?>From the above code we can see that although we modified the first element of the new array, the original array remains unchanged.
In summary, the array_values() function is a very practical array handling function that returns a new array containing all elements, works with both indexed and associative arrays, and can improve code readability and maintainability when used appropriately in PHP development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
