Thursday 15 April 2010

php sort multidimensional array key ascending -



php sort multidimensional array key ascending -

i have array:

array => ( [0] => array( [a] => hello, [b] => world ), [1] => array( [a] => bye, [b] => planet ), ..... )

and need function sort this:

array => ( [0] => array( [a] => bye, [b] => planet ), [1] => array( [a] => hello, [b] => world ), ..... )

been hours trying , going mad, please help me.

thanks!!

if mean sort array based on contents of strings in array, you're going have apply logic sort. using usort allows pass in arbitrary function perform comparison.

usort($my_array, function ($a, $b) { homecoming strcasecmp(implode($a), implode($b)); });

this way, it'll compare 2 arrays so:

array 1 = [ 'foo', 'bar' ] array 2 = [ 'baz', 'quux' ] array 1 converted "foobar" array 2 converted "bazquux" compare strings "foobar" "bazquux" -> "bazquux" comes first alphabetically, strcasecmp() homecoming positive integer -> usort receives positive integer informs sorting algorithm

php arrays sorting multidimensional-array

No comments:

Post a Comment