how to sort an array based on previous numerical keys? [duplicate]

Home / Uncategorized / how to sort an array based on previous numerical keys? [duplicate]

Question:
This question already has an answer here:Sort array keys ascending 3 answers

I would like to sort an array to reindex its keys. I tried using custom loop but it does not work. Is there a php function to do this? I have an array like this:Array
(
[3] => Array
(
[id] => 44
[name] => 1495714823.jpg
)
[1] => Array
(
[id] => 48
[name] => 1495795384.jpg
)
[2] => Array
(
[id] => 11
[name] => 1495602099.jpg
)
)

I need to sort array like this:Array
(
[1] => Array
(
[id] => 48
[name] => 1495795384.jpg
)
[2] => Array
(
[id] => 11
[name] => 1495602099.jpg
)
[3] => Array
(
[id] => 44
[name] => 1495714823.jpg
)
)

i tried some of the sort functions but it didn’t work.


Answer:
I think you need ksort
ksort($array);

This should give you the output you are looking for.
See here for output: https://3v4l.org/hgv4F
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *