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

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

Linda Hamilton
Linda HamiltonOriginal
2024-11-08 22:34:02831browse

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>

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>

The resulting $reindexedArray will have the following structure:

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

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!

Statement:
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