php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62840 Add sort flag to ArrayObject::ksort
Submitted: 2012-08-16 15:23 UTC Modified: 2012-08-21 20:11 UTC
From: bert at becoded dot be Assigned: laruence (profile)
Status: Closed Package: SPL related
PHP Version: 5.4.5 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bert at becoded dot be
New email:
PHP Version: OS:

 

 [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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-21 00:59 UTC] reeze dot xia at gmail dot com
Sounds reasonable to me I am working on a patch for this request.
Will send a pull request later.

Thanks
 [2012-08-21 05:30 UTC] laruence@php.net
hmm, after a quick look, this will be very easy to implemented. then I did it. :)
 [2012-08-21 05:30 UTC] laruence@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: laruence
 [2012-08-21 05:30 UTC] laruence@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2012-08-21 05:59 UTC] reeze dot xia at gmail dot com
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
 [2012-08-21 06:24 UTC] laruence@php.net
yes, I noticed that, just I thought it was acceptable, there is the same issue in 
uasort.
 [2012-08-21 20:11 UTC] bert at becoded dot be
Thx for the quick implementation!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC