Working only on a copy of an array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Working only on a copy of an array?

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post 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.
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post 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);
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what about using usort() :?:
Post Reply