guigoz
guigoz

Reputation: 704

arsort does not give the expected result

$a=array('a'=>'`a:1:`','b'=>'`a:`','c'=>'`a:0:`');
arsort($a);
print_r($a);

I expect this code to output

Array
(
    [a] => `a:1:`
    [c] => `a:0:`
    [b] => `a:`
)

but it actually outputs

Array
(
    [b] => `a:`
    [a] => `a:1:`
    [c] => `a:0:`
)

Do you understand why the backticks are messing up ?

Upvotes: 0

Views: 49

Answers (1)

ChristianM
ChristianM

Reputation: 1823

The backtick will be considered as character for sorting. As all start with one that is not an isuse but the length of strings is different so the last backtick is considered in your case. See example data, until after the : all values are the same, so the next chars to sort by are backtick, 1 and 0, and those are ordered reverse, which seems correct to me.

Giving my comment from above as answer, because I guess it's the right answer.

To extend it, you might need to do a uasort using trim to remove the backticks and sorting reversely then.

Upvotes: 1

Related Questions