Page 1 of 1

Working only on a copy of an array?

Posted: Thu Mar 03, 2005 3:15 pm
by danf_1979
Im working with some arrays and sorting its values with multisort.
I have an initial array that i want to sort several times in different places of my script (always from the original array), but for the second time I want to sort it is already sorted by the first sort operation.
Code is something like this:

Code: Select all

$rank1 = $vendedor;
//Fisrt Ranking 
array_multisort($neto, SORT_DESC, $rank1);
.
.
.
.
$rank2 = $vendedor;
//Second Ranking
array_multisort($ventas, SORT_DESC, $rank2);
and i thought I would be only sorting rank1 and rank2, just copies of array $vendedor, but when I look at results I see $vendedor array sorted as well.. Any way of just sorting a copy of an array and not the original array?
Thank you.

Posted: Thu Mar 03, 2005 3:19 pm
by feyd
the first argument is passed via reference.. you need to make a copy of the array if you want differing sorts at the same time.. or just resort it back after processing at the given point..

Posted: Thu Mar 03, 2005 3:28 pm
by danf_1979
Thank you for your answer, but I thought I could just copy an array with the = operator. Some keywords you could give me for searching some more info or functions to copy an array? I' m searching php.net by keyworkds "copy array" but i'm not being lucky though...
Thank you.

Posted: Thu Mar 03, 2005 3:36 pm
by danf_1979
Ok, so I needed to use &$rank to pass the values only as reference. Got it in google. Thank you.

Code: Select all

$rank2 = $vendedor;
//Ranking Venta NETA
array_multisort($ventas, SORT_DESC, &$rank2);

Posted: Thu Mar 03, 2005 3:48 pm
by danf_1979
I got this warning:

Code: Select all

Warning: Call-time pass-by-reference has been deprecated - argument passed 
by value; If you would like to pass it by reference, modify the declaration of array_multisort(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer. in c:\archivos de programa\easyphp1-7\www\proyectos\evaluacion\vendedores.php on line 310
Should I enable it on php.ini?

Posted: Thu Mar 03, 2005 3:51 pm
by feyd
what about using usort() :?: