This question already has an answer here:
- sort array values by key using php 2 answers
- Array case-insensitive key sort in PHP version 5.3 or less 2 answers
- Array sorting case-sensitivity 3 answers
I have an array like this with alphabetical keys :
Array
(
[0] => Array
(
[UserID] => 1
[EmailAddress] => user5@gmail.com
[TransID] => fjhf8f7848
)
[1] => Array
(
[UserID] => 1
[EmailAddress] => johndoe@gmail.com
[TransID] => dfsdhsdu78
)
)
I want to sort this array in alphabetical order of the keys. Expected output is :
Array
(
[0] => Array
(
[EmailAddress] => user5@gmail.com
[TransID] => fjhf8f7848
[UserID] => 1
)
[1] => Array
(
[EmailAddress] => johndoe@gmail.com
[TransID] => dfsdhsdu78
[UserID] => 2
)
)
I tried various array sort functions but they return blank.
How do I sort such a array with alphabetical keys in alphabetic order?
You can use array_map and ksort,
Demo.
Using foreach loop,
EDIT
In that case you can use,
Demo
Output