ID: 17307
Updated by: [EMAIL PROTECTED]
Reported By: [EMAIL PROTECTED]
Status: Feedback
Bug Type: Arrays related
Operating System: Linux
PHP Version: 4.2.1
New Comment:
<?php
/*
let's define some array elements
For example, 3 products (A,B,C) with different price (livre)
*/
$elt["nom"]="A"; $elt["livre"]=10; $elt["quantite"]=1; $ar[]=$elt;
$elt["nom"]="A"; $elt["livre"]=9; $elt["quantite"]=1; $ar[]=$elt;
$elt["nom"]="A"; $elt["livre"]=7; $elt["quantite"]=1; $ar[]=$elt;
$elt["nom"]="A"; $elt["livre"]=11; $elt["quantite"]=1; $ar[]=$elt;
$elt["nom"]="A"; $elt["livre"]=101; $elt["quantite"]=1; $ar[]=$elt;
$elt["nom"]="B"; $elt["livre"]=104; $elt["quantite"]=1; $ar[]=$elt;
$elt["nom"]="C"; $elt["livre"]=101; $elt["quantite"]=1; $ar[]=$elt;
/*
Our sort function only swap 2 values if the name is the same and the
first price is higher than second
As we give a 'nom' field sorted alphabetically array, we might get the
result : A7
A9
A10
A11
A101
B104
C101
Which is the result in php 4.0.6
But the result un php 4.2.1 is :
C101
B104
A7
A9
A10
A11
A101
*/
function sort_function($a, $b)
{
if ((($a["livre"]/$a["quantite"]) == ($b["livre"]/$b["quantite"])) ||
$a["nom"] != $b["nom"]) return 0;
return (($a["livre"]/$a["quantite"]) < ($b["livre"]/$b["quantite"])) ?
-1 : 1; }
/*
This is a 2nd function, which always returns that elements are equal.
So it might not change the array...
If you try this alternate function, you'll see that the array is
modified
to :
B104
C101
A101
A11
A9
A7
A10
??????! really strange behaviour
*/
function sort_function_void($a, $b)
{
return 0;
}
echo "<HTML><PRE>UnSorted:\n";
for ($i=0;$i<sizeof($ar);$i++)
echo $ar[$i]["nom"].$ar[$i]["livre"]."\n";
usort($ar, sort_function);
//usort($ar, sort_function_void);
echo "Sorted:\n";
for ($i=0;$i<sizeof($ar);$i++)
echo $ar[$i]["nom"].$ar[$i]["livre"]."\n";
?>
Previous Comments:
------------------------------------------------------------------------
[2002-05-22 08:45:20] [EMAIL PROTECTED]
Huh ... Related to Bug�#17257
;)
------------------------------------------------------------------------
[2002-05-22 08:44:49] [EMAIL PROTECTED]
Related to Bug�#17307
------------------------------------------------------------------------
[2002-05-18 08:49:39] [EMAIL PROTECTED]
Please prive a short self containing script (which runs right away
after copy&paste).
Derick
------------------------------------------------------------------------
[2002-05-18 08:36:08] [EMAIL PROTECTED]
I used to make sorts with usort and calling the function below. This
was working on the last versions but not anymore on the 4.2.1 :/ which
outputs strange result.
function sort_function($a, $b)
{
if ((($a["livre"]/$a["quantite"]) == ($b["livre"]/$b["quantite"])) ||
$a["nom"] != $b["nom"]) return 0;
return (($a["livre"]/$a["quantite"]) < ($b["livre"]/$b["quantite"]))
? -1 : 1;
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=17307&edit=1