|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #13825 Change sort functions to return sorted array instead of modifying existing
Submitted: 2001-10-25 11:38 UTC Modified: 2017-01-29 04:22 UTC
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: lee at mediawaveonline dot com Assigned:
Status: No Feedback Package: Arrays related
PHP Version: * 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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: lee at mediawaveonline dot com
New email:
PHP Version: OS:


 [2001-10-25 11:38 UTC] lee at mediawaveonline dot com
$test[0] = 0;
$test[1] = 1;
$test[2] = 2;

I hate this, a function should never modify a variable, it should return the new modified variable. they syntax should be

$test = asort($test);

there are good reasons or this. I hate needing a variable sorted for one command and having to make a temp var just todo that.

$tmp = $test;
foreach( $tmp as $pos => $val )

where it could be.

foreach( asort($test) as $pos => $val )

saves time and ledgibility. I understand its not easy to change the syntax now. if you wanted to make some work or yourself you could make this a php.ini option. if you want something simpler on your end, just make a new set of functions, thats what Ive done.

function my_asort($array)
  return $array);

even if you dont want to ever include this into base php, at least make asort return the newly modified variable.

$tmp = $test;
foreach( asort($tmp) as $pos => $val )

  Chris Lee


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-22 07:26 UTC] jo at durchholz dot org
I agree that there should be functions that return a sorted array instead of sorting in-place. Given the way that PHP handles variables, this should be no more than moderately inefficient.

I also find the multitude of sort functions confusing. I always have to look them up. I'd suggest a sort function like this:

sorted ($array, $flags, $compare_fn = NULL, $compare_flags = 0)

where $flags may be combined from one of each of the following lines (first flag is the default):
    These flags define how the values to be sorted (whether it's keys
    or values) are to be transformed before comparing.
    SORT_NUMERIC converts the values to be sorted to numbers.
    SORT_STRING converts them to strings.
    SORT_KEYS has the effect of ksort.
    SORT_VALUES_KEYS sorts by values, resorting to keys if values are
    SORT_KEYS_VALUES sorts by key, then value. This can make sense
    with SORT_NUMERIC or SORT_STRING if two keys get mapped to the
    same value.
    Whether to reindex the result array numerically or keep the
    key-value associations. (Numeric keys probably need to be
    reindexed always, but I don't know all ramifications here.)
    SORT_KEEP_KEYS gives the effect of asort.
    SORT_REVERSE has the effect of rsort.

$compare_fn ($a, $b, $flags) will get the values to be compared in $a and $b, and sorted()s $compare_flags in $flags.
For SORT_VALUES, $a and $b will be values from $array.
For SORT_KEYS, $a and $b will be keys from $array.
For SORT_VALUES_KEYS, $a and $b will be arrays ($value, $key) from a $value=>$key pair in $array.
For SORT_KEYS_VALUES, $a and $b will be arrays ($key, $value) from a $value=>$key pair in $array.

For $compare_flags, the only flag that could be predefined is SORT_LOCALE.

If no $compare_fn is given, the equivalent of this PHP code is executed:
  if (is_array ($a)) {
    $result = _compare ($a [0], $b [0]);
    if ($result == 0) {
      return _compare ($a [1], $b [1]);
    } else {
      return $result;
  } else {
    return _compare ($a, $b);
where _compare is defined to return -1, 0, or +1 depending on how its two parameters compare.

Hope this is useful :-)
 [2010-12-31 22:47 UTC]
-Summary: sort functions +Summary: Change sort functions to return sorted array instead of modifying existing -Package: Feature/Change Request +Package: Arrays related -Operating System: linux, windows +Operating System: * -PHP Version: 4.0.6 +PHP Version: *
 [2017-01-20 17:18 UTC]
-Status: Open +Status: Feedback
 [2017-01-20 17:18 UTC]
Is this still relevant?
 [2017-01-29 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 28 01:01:31 2024 UTC