arrays - Why is the output by PHP natsort() function wrong in the following situation? -
i reading natsort() on php manual. show examples headline "natsort() examples demonstrating potential gotchas". think means out of natsort()
different in these examples supposed be. here code:
<?php echo "negative numbers\n"; $negative = array('-5','3','-2','0','-1000','9','1'); natsort($negative); print_r($negative); echo "zero padding\n"; $zeros = array('09', '8', '10', '009', '011', '0'); natsort($zeros); print_r($zeros); ?>
the output in first case is:
array ( [2] => -2 [0] => -5 [4] => -1000 [3] => 0 [6] => 1 [1] => 3 [5] => 9 )
i understand wrong because negative number not sorted smaller bigger.
here output of second example:
array ( [5] => 0 [1] => 8 [0] => 09 [3] => 009 [2] => 10 [4] => 011 )
what output supposed in case? me, seemed right order. numbers in ascending order. explanation me understand function , avoid mistakes when using in future.
thanks.
Comments
Post a Comment