user1869566
user1869566

Reputation: 110

PHP array rename keys

I have an array like this, the keys are epoch timestamps they were used to order the files by date i now want to rename the keys to 0, 1, 2, 3, etc

Array($files)
(
    [1365168386] => _MG_5704.jpg
    [1368201277] => _MG_5702.jpg
    [1368201719] => jetty.jpg
    [1368202375] => _MG_6100.jpg
    [1368202758] => _MG_5823.jpg
    [1368203032] => _MG_5999.jpg
    [1368203244] => _MG_5794.jpg
    [1368203477] => _MG_5862.jpg
    [1368203727] => _MG_6028.jpg
)

so it becomes

Array($files)
(
    [0] => _MG_5704.jpg
    [1] => _MG_5702.jpg
    [2] => jetty.jpg
    [3] => _MG_6100.jpg
    [4] => _MG_5823.jpg
    [5] => _MG_5999.jpg
    [6] => _MG_5794.jpg
    [7] => _MG_5862.jpg
    [8] => _MG_6028.jpg
)

Upvotes: 0

Views: 181

Answers (2)

deco20
deco20

Reputation: 35

$files = array_map('array_values', $files);

This will reset all your key values in your array.

Upvotes: 0

bwoebi
bwoebi

Reputation: 23777

array_values returns a numeric array, starting from 0: http://php.net/array_values

$files = array_values($files);

array_values also maintains the order.

Upvotes: 6

Related Questions