php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19617 Array_multisort don't work on GLOBAL array
Submitted: 2002-09-26 10:13 UTC Modified: 2003-09-08 08:48 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (25.0%)
From: jpapin at free dot fr Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.3-dev 5.0.0b2 OS: Unix
Private report: No CVE-ID: None
 [2002-09-26 10:13 UTC] jpapin at free dot fr
Using array_multisort in a function don't work if the array 
is "global" (you can paste the following code) :

<HTML>
   <HEAD>
      <TITLE></TITLE>
   </HEAD>
<body>
<?
function t() {
   global $row;
   $file = fopen("file.txt","r") or die;
   while (!feof($file)) {
      $row[] = fgetcsv($file,80,";");
   }
   /* $row[] like this
      Array ( [0] => Array ( [0] => 406 
                             [1] => xxxxxxxxxxxxxxxxxxxx
                             [2] => 5,72 )
              [1] => Array ( [0] => 001
                             [1] => zzzzzzzzzzzzzzzzzzzzz
                             [2] => 5,77 )
              [2] => Array ( [0] => 402
                             [1] => aaaaaaaaaaaaaaaaaaaaa
                             [2] => 13,71 )
              [3] => Array ( [0] => 009
                             [1] => fffffffffffffffffffff
                             [2] => 7,61 )
            ) 
     I want to sort it on aaa.../fff.../xxx.../zzz...
     */       
   fclose($file); 
   foreach ( $row as $value ) {
      $sortarray[] = $value[1];
   }
   /* $sortarray like this
      Array ( [0] => xxxxxxxxxxxxxxxxxxxx
              [1] => zzzzzzzzzzzzzzzzzzzz
              [2] => aaaaaaaaaaaaaaaaaaaa
              [3] => ffffffffffffffffffff
            ) 
   */
   // I try to use array_multisort($row,$sortarray); too
   array_multisort($sortarray,$row);
   print_r($row);
   // no change for $row[], until i remove global for $row;
   echo "<br>";
   print_r($sortarray);
   /* $sortarray[], now like this 
      Array ( [0] => aaaaaaaaaaaaaaaaaaaa
              [1] => ffffffffffffffffffff
              [2] => xxxxxxxxxxxxxxxxxxxx
              [3] => zzzzzzzzzzzzzzzzzzzz
            ) 
     or no change if set to global too.       
   */
}
t();
?>
</body>
</HTML>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-09-26 10:42 UTC] sniper@php.net
Please provide a short (!) complete and self-containing example script. You should also try with the 4.2.3 release.

 [2002-09-26 11:11 UTC] jpapin at free dot fr
Sorry, try this with and without "global $row;"

<?
function t() {
   global $row;
   $row = array(array("1","Php"),array(2,"I"),array(3,"Love"));
   foreach ( $row as $value ) {
      $sortarray[] = $value[1];
   }
   array_multisort($sortarray,$row);
   print_r($row);
   echo "<br>";
   print_r($sortarray);
}
t();
?>
 [2002-09-26 11:14 UTC] sniper@php.net
Works fine here with latest CVS head.
Please, try PHP 4.2.3. (4.2.0 is really too old..)

 [2003-05-13 07:03 UTC] matschek at gmx dot de
Tested with PHP 4.3.1, same problem:
Globalized arrays cannot be sorted using array_multisort, although the functions returns TRUE!

Another short piece of code to test:

<?
  function test() {
    global $data;
    $data= array("first", "fifth", "second", "forth", "third");
    $sort= array(1,5,2,4,3);
    array_multisort($sort, $data);
    print_r($data);
  }

  test();
?>

Without "global $data;" it works fine.
 [2003-05-13 07:19 UTC] matschek at gmx dot de
Another hint:
if you pass the $data as a reference [for my example above: 'array_multisort($sort, &$data);'], it works - but this is deprecated, so this cant be the solution..
 [2003-09-08 08:48 UTC] sniper@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. Because of this, we hope you add your comments
to the existing bug instead.

Thank you for your interest in PHP.

See bug #25359 (more recent)


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 17 18:01:29 2024 UTC