note 85046 added to function.ksort
| From: | maik dot riechert at animey dot net | Date: | Tue, 12 Aug 2008 18:32:47 +0000 |
| Subject: | note 85046 added to function.ksort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143419@lists.php.net to get a copy of this message | ||
Be careful when using ksort for mixed type keys!!
$a = array(
'first' => true,
0 => 'sally',
);
$b = array(
0 => 'sally',
'first' => true,
);
ksort($a);
ksort($b);
var_dump($a);
var_dump($b);
Output is:
array(
0 => 'sally',
'first' => true,
)
array(
'first' => true,
0 => 'sally',
)
If you want same results for both arrays, use:
ksort($a, SORT_STRING);
The reason for that lays in the compare mechanism which would normally just typecast
'first' to an integer or 0 to a string when comparing it to each other. So you have to use
SORT_STRING, otherwise you would lose information when 'first' is converted to int.
----
Server IP: 217.13.201.10
Probable Submitter: 217.68.166.44
----
Manual Page -- http://www.php.net/manual/en/function.ksort.php
Edit -- https://master.php.net/note/edit/85046
Del: integrated -- https://master.php.net/note/delete/85046/integrated
Del: useless -- https://master.php.net/note/delete/85046/useless
Del: bad code -- https://master.php.net/note/delete/85046/bad+code
Del: spam -- https://master.php.net/note/delete/85046/spam
Del: non-english -- https://master.php.net/note/delete/85046/non-english
Del: in docs -- https://master.php.net/note/delete/85046/in+docs
Del: other reasons-- https://master.php.net/note/delete/85046
Reject -- https://master.php.net/note/reject/85046
Search -- https://master.php.net/manage/user-notes.php