php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54968 arsort
Submitted: 2011-06-01 09:24 UTC Modified: 2011-06-01 17:58 UTC
From: jens at freude-now dot de Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.6 OS: OS X 10.6
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: jens at freude-now dot de
New email:
PHP Version: OS:

 

 [2011-06-01 09:24 UTC] jens at freude-now dot de
Description:
------------
---
From manual page: http://www.php.net/function.arsort#Rückgabewerte
---
I expected if I want to sort an empty array that the functions gives back a false because there is nothing to sort. 
But the functions gives back a true.

Test script:
---------------
$t = array();
echo '<pre>count($t) ::';
print_r( count($t));
echo '</pre>';

if(arsort($t)) {
    echo '<pre>$t ::';
    print_r( $t);
    echo '</pre>';
}


Expected result:
----------------
false

Actual result:
--------------
true

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-06-01 09:53 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2011-06-01 09:53 UTC] rasmus@php.net
Sorting 0 elements is not a failure case. It is perfectly valid for an array to 
have no elements and for this case to not cause a problem in the code. Otherwise 
you would always have to check the number of elements in the array before trying 
to sort it if you want to avoid errors.
 [2011-06-01 10:46 UTC] jens at freude-now dot de
mmh, imagine we, you and me, want to sort the coins in our pocket. But we don't have any coins. Can you sort it? I can't.
 [2011-06-01 17:58 UTC] rasmus@php.net
Of course I can. My array of 0 coins is perfectly sorted. There are no coins out 
of order. By your argument the function should also fail if you have 1 coin since 
that is another case where your coins are already sorted and the function has 
nothing to do.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Aug 20 04:00:03 2025 UTC