Wednesday, August 1, 2018

Sort php multidimensional array by sub-value




I have this array



Array
(

[data] => Array
(
[0] => Array
(

[id] => 1293005125
[viewed] => TRUE
[active] => TRUE
[time] => December 22, 2010 13:00 hours
[timestamp] => 1293006034

[initial_timestamp] => 1293005125
[user] => administrator
)

[1] => Array
(

[mid] => 1293001908
[viewed] => TRUE
[active] => TRUE

[time] => December 22, 2010 13:00 hours
[timestamp] => 1293001908
[initial_timestamp] => 1293001908
[user] => administrator
)

[2] => Array
(

[mid] => 1293009999

[viewed] => TRUE
[active] => TRUE
[time] => December 22, 2010 13:00 hours
[timestamp] => 1293009999
[initial_timestamp] => 1293009999
[user] => administrator
)

[3] => Array
(


[mid] => 1293006666
[viewed] => TRUE
[active] => TRUE
[time] => December 22, 2010 13:00 hours
[timestamp] => 1293006666
[initial_timestamp] => 1293006666
[user] => administrator
)


[4] => Array
(

[mid] => 1293005125
[viewed] => TRUE
[active] => TRUE
[time] => December 22, 2010 13:00 hours
[timestamp] => 1293006125
[initial_timestamp] => 1293005125
[user] => administrator2

)


)


Now I would like to sort this array by [mid]
How do I do this?



Currently I sort this in a foreach loop
There has to be a better way




EDIT
I hoped to output something like



[mid] key => array value



Thanks


Answer



You can use the usort function.




function cmp($a, $b) {
return $a["mid"] - $b["mid"];
}
usort($arr, "cmp");


See it


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...