php.net |  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
Votes:2
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
 [2001-10-25 11:38 UTC] lee at mediawaveonline dot com
$test[0] = 0;
$test[1] = 1;
$test[2] = 2;
asort($test);

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;
asort($tmp);
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)
{
  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
  lee@mediawaveonline.com

Patches

Pull Requests

History

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):
  SORT_REGULAR/SORT_NUMERIC/SORT_STRING
    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_VALUES/SORT_KEYS/SORT_VALUES_KEYS/SORT_KEYS_VALUES
    SORT_KEYS has the effect of ksort.
    SORT_VALUES_KEYS sorts by values, resorting to keys if values are
    equal.
    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.
  SORT_REINDEX/SORT_KEEP_KEYS
    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_REGULAR/SORT_REVERSE
    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:
<?php
  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] jani@php.net
-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] heiglandreas@php.net
-Status: Open +Status: Feedback
 [2017-01-20 17:18 UTC] heiglandreas@php.net
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: Sat Dec 21 14:01:32 2024 UTC