Edit report at https://bugs.php.net/bug.php?id=62840&edit=1

 ID:                 62840
 Comment by:         reeze dot xia at gmail dot com
 Reported by:        bert at becoded dot be
 Summary:            Add sort flag to ArrayObject::ksort
 Status:             Open
 Type:               Feature/Change Request
 Package:            SPL related
 PHP Version:        5.4.5
 Block user comment: N
 Private report:     N

 New Comment:

Sounds reasonable to me I am working on a patch for this request.
Will send a pull request later.

Thanks


Previous Comments:
------------------------------------------------------------------------
[2012-08-16 15:23:36] 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)


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=62840&edit=1

Reply via email to