php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68897 Using variable for sort flag in array_multisort cause wrong results
Submitted: 2015-01-24 05:51 UTC Modified: 2015-01-24 12:35 UTC
From: jrbasso at gmail dot com Assigned: laruence (profile)
Status: Closed Package: Arrays related
PHP Version: master-Git-2015-01-24 (Git) OS: Linux
Private report: No CVE-ID: None
 [2015-01-24 05:51 UTC] jrbasso at gmail dot com
Description:
------------
Using master rev. be5337421680f059f7b23dd530645e070b15715a the array_multisort have incorrect behavior if the SORT direction flag is passed in a variable.

This behavior is ok on HHVM and until PHP 5.6. See http://3v4l.org/7pU0u

Test script:
---------------
// Works
$values = array("Lindsay", "Nate");
array_multisort($values, SORT_DESC, SORT_REGULAR, array(0, 1), SORT_DESC, SORT_REGULAR);
var_dump($values);

// Doesn't work
$values = array("Lindsay", "Nate");
$dir = SORT_DESC;
array_multisort($values, $dir, SORT_REGULAR, array(0, 1), $dir, SORT_REGULAR);
var_dump($values);

Expected result:
----------------
array(2) {
  [0]=>
  string(4) "Nate"
  [1]=>
  string(7) "Lindsay"
}
array(2) {
  [0]=>
  string(4) "Nate"
  [1]=>
  string(7) "Lindsay"
}

Actual result:
--------------
array(2) {
  [0]=>
  string(4) "Nate"
  [1]=>
  string(7) "Lindsay"
}
array(2) {
  [0]=>
  string(7) "Lindsay"
  [1]=>
  string(4) "Nate"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-24 12:35 UTC] kalle@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: laruence
 [2015-01-24 12:35 UTC] kalle@php.net
I think the behavior was undefined, right Laruence?
 [2015-01-24 14:52 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a98fd7125ac9165822fa34a01e35a97a2076c3b9
Log: Fixed bug #68897 (Using variable for sort flag in array_multisort cause wrong results)
 [2015-01-24 14:52 UTC] laruence@php.net
-Status: Assigned +Status: Closed
 [2016-07-20 11:39 UTC] davey@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a98fd7125ac9165822fa34a01e35a97a2076c3b9
Log: Fixed bug #68897 (Using variable for sort flag in array_multisort cause wrong results)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 08:01:30 2024 UTC