"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Re-Index Array Values to Numeric Indices in PHP?

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

Published on 2024-11-18
Browse:169

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

Re-Indexing Array Values in PHP

Consider the following array with associative keys:

$array = [
    'id' => 3,
    'user_id' => 1,
    'clan_id' => 1,
    // ...
    'skill25xp' => 13373505
];

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.

$reindexedArray = array_values($array);

The resulting $reindexedArray will have the following structure:

Array (
    [0] => 3
    [1] => 1
    [2] => 1
    // ...
    [24] => 13373505
)

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3