| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             [2008-09-04 17:44 UTC] php at sameprecision dot org
  | 
    |||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 07:00:01 2025 UTC | 
Description: ------------ Using usort, not all pairs of elements are compared. If a comparison operation is not transitive, this leads to unexpected ordering, even though every pair of elements are comparable. This should probably be mentioned in the documentation. Reproduce code: --------------- //goal: sort $array so that an element is not a substring of any subsequent elements $array = array('aa','b','a'); //if $a is a substring of $b, return 1. Else return -1 function compare($a,$b){ return strpos($b,$a)===false ? -1 : 1; } usort($array,'compare'); print_r($array); Expected result: ---------------- Array ( [0] => aa [1] => b [2] => a ) Actual result: -------------- Array ( [0] => a [1] => b [2] => aa )