|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-08-16 15:23 UTC] bert at becoded dot be
Description: ------------ When mixing numeric and strings for the keys, you can get unexpected results. With the ksort, you can specify a sort flag (see the flags on http://www.php.net/manual/en/function.sort.php). Using that, you are able to change the sort behaviour. It would be great if we could specify the same sort flag for ArrayObject::ksort. So instead of public void ArrayObject::ksort ( void ) it would be public void ArrayObject::ksort ( [ int $sort_flags = SORT_REGULAR ]) Test script: --------------- <?php $list = array('key_3' => 'Value 3', 'Unknown key 1', 'key_1' => 'Value 1', 'key_2' => 'Value 2', 'key_0' => 'Value 0', 'Unknown key 2'); ksort($list); var_dump($list); ksort($list, SORT_STRING); var_dump($list); $list = new ArrayObject(); $list->offsetSet('key_3', 'Value 3'); $list->append('Unknown key 1'); $list->offsetSet('key_1', 'Value 1'); $list->offsetSet('key_2', 'Value 2'); $list->offsetSet('key_0', 'Value 0'); $list->append('Unknown key 2'); $list->ksort(); //$list->ksort(SORT_STRING); var_dump($list); Expected result: ---------------- If $list->ksort(SORT_STRING) would work, then I would expect object(ArrayObject)[1689] string 'Unknown key 1' (length=13) string 'Unknown key 2' (length=13) public 'key_0' => string 'Value 0' (length=7) public 'key_1' => string 'Value 1' (length=7) public 'key_2' => string 'Value 2' (length=7) public 'key_3' => string 'Value 3' (length=7) Actual result: -------------- //ksort array (size=6) 'key_0' => string 'Value 0' (length=7) 'key_1' => string 'Value 1' (length=7) 0 => string 'Unknown key 1' (length=13) 'key_2' => string 'Value 2' (length=7) 'key_3' => string 'Value 3' (length=7) 1 => string 'Unknown key 2' (length=13) //ksort with sort flag SORT_STRING array (size=6) 0 => string 'Unknown key 1' (length=13) 1 => string 'Unknown key 2' (length=13) 'key_0' => string 'Value 0' (length=7) 'key_1' => string 'Value 1' (length=7) 'key_2' => string 'Value 2' (length=7) 'key_3' => string 'Value 3' (length=7) //ArrayObject::ksort object(ArrayObject)[1689] public 'key_0' => string 'Value 0' (length=7) public 'key_1' => string 'Value 1' (length=7) string 'Unknown key 1' (length=13) public 'key_2' => string 'Value 2' (length=7) public 'key_3' => string 'Value 3' (length=7) string 'Unknown key 2' (length=13) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 15:00:01 2025 UTC |
cool, I implemented it last night, but missing a better test case. I think the error message isn't correct, so I didn't sent it. you could see that, the warning message is not really exactly correct. It just a proxy to call asort($obj, $flag) if the $flag is not valid. It complained about the 2 parameter is not valid but not the first parameter. $obj->sort('adfsd'); // 1 sort($ar, 'adfds'); // 2