Home > Backend Development > PHP Tutorial > How to Re-Index Array Values to Numeric Indices in PHP?

How to Re-Index Array Values to Numeric Indices in PHP?

Linda Hamilton
Release: 2024-11-08 22:34:02
Original
901 people have browsed it

How to Re-Index Array Values to Numeric Indices in PHP?

Re-Indexing Array Values in PHP

Consider the following array with associative keys:

<code class="php">$array = [
    'id' => 3,
    'user_id' => 1,
    'clan_id' => 1,
    // ...
    'skill25xp' => 13373505
];</code>
Copy after login

To re-index the keys to numeric indices starting from 0, you can use the array_values() function, which returns a new array with sequential indices.

<code class="php">$reindexedArray = array_values($array);</code>
Copy after login

The resulting $reindexedArray will have the following structure:

<code class="php">Array (
    [0] => 3
    [1] => 1
    [2] => 1
    // ...
    [24] => 13373505
)</code>
Copy after login

The array_values() function effectively removes the original keys and assigns new sequential indices to the values. This process is useful when you need to ensure your array has consecutive numeric keys, making it easier to iterate over or access specific elements by index.

The above is the detailed content of How to Re-Index Array Values to Numeric Indices in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template