php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39456 Problem not in array_multisort, but in call_user_func_array
Submitted: 2006-11-10 09:36 UTC Modified: 2006-11-10 11:06 UTC
From: ricardo dot matters at mindbench dot nl Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.0 OS: debian cur 2.6.18-1-686-bigmem
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: ricardo dot matters at mindbench dot nl
New email:
PHP Version: OS:

 

 [2006-11-10 09:36 UTC] ricardo dot matters at mindbench dot nl
Description:
------------
To sort an multidimensional array, I'm using a wrapper for array_multisort. Was working just fine, but as from 5.2, nothing is being sorted. Documentation lacks any information if array_multisort has been changed.

Reproduce code:
---------------
function multisort() {
  $n = func_num_args();
  $ar = func_get_arg($n-1);
  if (!is_array($ar))
    return false;

  for ($i = 0; $i < $n-1; $i++) {
    $col[$i] = func_get_arg($i);
  }

  foreach($ar as $key => $val) {
    foreach($col as $kkey => $vval) {
      if (is_string($vval)) {
        ${"subar$kkey"}[$key] = $val[$vval];
      }
    }
  }

  foreach($col as $key => $val) {
    $arv[] = (is_string($val) ? ${"subar$key"} : $val);
  }
  $arv[] = $ar;

  call_user_func_array("array_multisort", $arv);
  return $ar;
}

$ar = array(
  array('id' => 1, 'weight' => 78, 'ts' => 123456),
  array('id' => 1, 'weight' => 78, 'ts' => 123457),
  array('id' => 1, 'weight' => 98, 'ts' => 134526),
  array('id' => 1, 'weight' => 10, 'ts' => 112456),
  array('id' => 1, 'weight' => 56, 'ts' => 177776)
);
print_r(multisort('weight', SORT_DESC, SORT_NUMERIC, 'ts', SORT_DESC, SORT_NUMERIC, $ar));

Expected result:
----------------
Array
(
    [0] => Array
        (
            [id] => 1
            [weight] => 98
            [ts] => 134526
        )
    [1] => Array
        (
            [id] => 1
            [weight] => 78
            [ts] => 123457
        )
    [2] => Array
        (
            [id] => 1
            [weight] => 78
            [ts] => 123456
        )
    [3] => Array
        (
            [id] => 1
            [weight] => 56
            [ts] => 177776
        )
    [4] => Array
        (
            [id] => 1
            [weight] => 10
            [ts] => 112456
        )

)


Actual result:
--------------
Array
(
    [0] => Array
        (
            [id] => 1
            [weight] => 78
            [ts] => 123456
        )

    [1] => Array
        (
            [id] => 1
            [weight] => 78
            [ts] => 123457
        )

    [2] => Array
        (
            [id] => 1
            [weight] => 98
            [ts] => 134526
        )

    [3] => Array
        (
            [id] => 1
            [weight] => 10
            [ts] => 112456
        )

    [4] => Array
        (
            [id] => 1
            [weight] => 56
            [ts] => 177776
        )

)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-10 09:50 UTC] tony2001@php.net
Please provide less complicated example. 
If there is a problem with array_multisort(), I believe you should be able to reproduce it with direct call.
 [2006-11-10 10:08 UTC] ricardo dot matters at mindbench dot nl
OK, I distilled some of the code, and found another curious aspect. If I run the code directly, the result is as expected. 
But if I put the same code in a function, it does not. Seems like a reference problem.

Working:
$weights = array(78,78,98,10,56);
$tss = array( 1,2,3,4,5);
$ar = array(
  array('weight' => 78, 'ts' => 1),
  array('weight' => 78, 'ts' => 2),
  array('weight' => 98, 'ts' => 3),
  array('weight' => 10, 'ts' => 4),
  array('weight' => 56, 'ts' => 5)
);

array_multisort($weights, SORT_DESC, SORT_NUMERIC, $tss, SORT_DESC, SORT_NUMERIC, $ar);
print_r($ar);

NOT working:
function multisort() {
  $weights = array(78,78,98,10,56);
  $tss = array( 1,2,3,4,5);
  $ar = array(
    array('weight' => 78, 'ts' => 1),
    array('weight' => 78, 'ts' => 2),
    array('weight' => 98, 'ts' => 3),
    array('weight' => 10, 'ts' => 4),
    array('weight' => 56, 'ts' => 5)
  );

  array_multisort($weights, SORT_DESC, SORT_NUMERIC, $tss, SORT_DESC, SORT_NUMERIC, $ar);
  return $ar;
}

multisort($ar);
print_r($ar);
 [2006-11-10 10:49 UTC] mgf@php.net
Basic support issue -- should really be on the php-general list. Nonetheless: You're not *doing* anything with the return value of multisort() - try

  $ar = multisort();

instead of

  multisort($ar);
 [2006-11-10 11:06 UTC] ricardo dot matters at mindbench dot nl
Sorry, that example was incorrect. I found the problem and fixed it, but I still think it's a bug, not in array_multisort but in call_user_func_array. If I use call_user_func_array($args) in the first code example, it fails. But if I use exactly the same parameters and use eval to execute it, the result is as expected. This code was working just fine in < 5.2, so there must have been some change to one of these functions, and is not stated in the manual.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 09 11:01:34 2025 UTC