0

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
  • Please show what you have tried yourself. Commented May 26, 2013 at 16:00
  • 1
    You want to hear about array_values. Commented May 26, 2013 at 16:01

2 Answers 2

6

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

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.
0
$files = array_map('array_values', $files);

This will reset all your key values in your array.

answered May 26, 2013 at 16:04

4 Comments

what? array_values on the string values of the array?
@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?
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...
@bwoebi array_values() expects parameter 1 to be array is what I was getting

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.