PHP bug in array_multisort()

Posted Wednesday, April 13, 2005 at 11h54 in Computers

This is a php bug post, so if you don't use php, stop reading now.

Be careful when using array_multisort() on copies of arrays, as you might end up changing the original array. I lost half a day to debugging as a result of this. Given the following code:

  1. $test1 = array(3,2,1);
  2. $test2 = $test1;
  3. $test3 = array('a', 'b', 'c');
  4.  
  5. array_multisort($test2, SORT_ASC, $test3);
  6.  
  7. echo 'test1:';
  8. print_r($test1);
  9. echo 'test2:';
  10. print_r($test2);
  11. echo 'test3:';
  12. print_r($test3);

You would expect:

  1. test1:Array
  2. (
  3.     [0] => 3
  4.     [1] => 2
  5.     [2] => 1
  6. )
  7. test2:Array
  8. (
  9.     [0] => 1
  10.     [1] => 2
  11.     [2] => 3
  12. )
  13. test3:Array
  14. (
  15.     [0] => c
  16.     [1] => b
  17.     [2] => a
  18. )

However, if you run the code, you actually get:

  1. test1:Array
  2. (
  3.     [0] => 1
  4.     [1] => 2
  5.     [2] => 3
  6. )
  7. test2:Array
  8. (
  9.     [0] => 1
  10.     [1] => 2
  11.     [2] => 3
  12. )
  13. test3:Array
  14. (
  15.     [0] => c
  16.     [1] => b
  17.     [2] => a
  18. )

Note that the original ($test1) ends up being sorted even though it was never called by array_multisort(). To work around this, insert a statement to modify the copy ($test2) before calling array_multisort() on it. The following code will produce the expected "correct" results:

  1. $test1 = array(3,2,1);
  2. $test2 = $test1;
  3. $test3 = array('a', 'b', 'c');
  4.  
  5. $test2[0] = $test2[0];                // fix
  6. array_multisort($test2, SORT_ASC, $test3);
  7.    
  8. echo 'test1:';
  9. print_r($test1);
  10. echo 'test2:';
  11. print_r($test2);
  12. echo 'test3:';
  13. print_r($test3);

This seems to be a resurrection of the closed bug #8130. Also, someone reported this behavior in bug #32031, but it was incorrectly labeled "bogus" in reference to bug #25359, which is a different issue. This was tested on PHP 5.0.4-dev.

Swing, jazz, and japanese schoolgirls

Posted Sunday, April 3, 2005 at 13h49 in Entertainment

Who would've thought to make a movie about a group of Japanese high school students who get together to form a jazz swing band? Apparently Shinobu Yaguchi did, the director of the movie Swing Girls, a film with warm, fluffy, and comedic layers that give the whimsically surreal story appeal to anyone with an inclination for inspirational feel-good movies. A comedy at heart, the film exhibits a carefree attitude, apparent in it's lack of effort in the areas of obsessive character development and dogmatic realism. But that does not matter, at least not in this movie. The characters develop their own charm and appeal without the aid of any sort of moving psychologically traumatic past histories, and the actors play well enough to pass as on screen musicians, at least to one who has played piano, drums, orchestral percussion, violin, and cello. In any case, the movie left me with a smile on my face and a musical jig in my heart (or rather, the cerebral mass that conveys emotions that are often thought of as feelings in the heart). And now I want a drum set.