php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20982 sort doesn't sort
Submitted: 2002-12-13 00:09 UTC Modified: 2002-12-13 02:25 UTC
From: tom at minnesota dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 4.2.2 OS: NetBSD/Alpha (64bit) - 1.6
Private report: No CVE-ID: None
 [2002-12-13 00:09 UTC] tom at minnesota dot com
# cat sort.php
<?php

$fruits = array ("lemon", "orange", "banana", "apple");
sort ($fruits);
reset ($fruits);
while (list ($key, $val) = each ($fruits)) {
    echo "fruits[".$key."] = ".$val."\n";
}

?>
# php -v
4.2.2
# php -q sort.php
fruits[0] = lemon
fruits[1] = orange
fruits[2] = banana
fruits[3] = apple
#

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-13 00:37 UTC] tom at minnesota dot com
I also tried other various sort functions and they all failed to sort:

# cat sort.php
<?php

// sort() demo
echo "\n";
$fruits = array ("lemon", "orange", "banana", "apple");
sort ($fruits);
reset ($fruits);
while (list ($key, $val) = each ($fruits)) {
    echo "fruits[".$key."] = ".$val."\n";
}

// asort() demo
echo "\n";
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
asort ($fruits);
reset ($fruits);
while (list ($key, $val) = each ($fruits)) {
    echo "$key = $val\n";
}

// ksort() demo
echo "\n";
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort ($fruits);
reset ($fruits);
while (list ($key, $val) = each ($fruits)) {
    echo "$key = $val\n";
}

// usort() demo
echo "\n";
$fruits = array();

function cmp ($a, $b) {
    return strcmp($a["fruit"], $b["fruit"]);
}

$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";

usort($fruits, "cmp");

while (list ($key, $value) = each ($fruits)) {
    echo "\$fruits[$key]: " . $value["fruit"] . "\n";
}

?>
# php -q sort.php

fruits[0] = lemon
fruits[1] = orange
fruits[2] = banana
fruits[3] = apple

d = lemon
a = orange
b = banana
c = apple

d = lemon
a = orange
b = banana
c = apple

$fruits[0]: lemons
$fruits[1]: apples
$fruits[2]: grapes
#
 [2002-12-13 02:25 UTC] sniper@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, 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/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Nov 30 12:03:35 2021 UTC