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
)
asked May 26, 2013 at 15:58
2 Answers 2
array_values returns a numeric array, starting from 0: http://php.net/array_values
$files = array_values($files);
array_values also maintains the order.
answered May 26, 2013 at 16:00
1 Comment
user1869566
Thanks, i'd done it using a loop but knew there was a function that would do it in one go but just couldn't find it.
$files = array_map('array_values', $files);
This will reset all your key values in your array.
4 Comments
bwoebi
what? array_values on the string values of the array?
deco20
@bwoebi correct me if I am wrong, it is late here haha, but the 'array_values' part is a callback function and will be applied to each array element in $files?
bwoebi
array_values expects an array as parameter, not the values of the array. But array_map passes the values of each entry to array_values. Try it yourself...
deco20
@bwoebi array_values() expects parameter 1 to be array is what I was getting
lang-php
array_values
.