ToonMariner Posted May 29, 2007 Share Posted May 29, 2007 I have a 2d array '$arr'. I copy the array... $arr2 = $arr; and then apply the array_mulisort to $tarr2. The result is that $arr2 is sorted as desired BUT $arr is now also sorted to the same specification as $arr2!!! What is going on - what am I missing... $arr2 = $arr; array_multisort ( $arr2['tip_date'] , SORT_ASC, SORT_STRING , $arr2['service_name'] , SORT_ASC, SORT_STRING , $arr2['service_id'] , SORT_ASC, SORT_NUMERIC , $arr2['tip_id'] , SORT_ASC, SORT_NUMERIC , $arr2['tip_point'] , SORT_ASC, SORT_NUMERIC , $arr2['tip_odds'] , SORT_ASC, SORT_NUMERIC , $arr2['result'] , SORT_ASC, SORT_STRING , $arr2['tip_text'] , SORT_ASC, SORT_STRING ); I am not sorting any other arrays anywhere else in the script and this code is in the main body of script not a function... Many thanks to the individual who points out where I am going wrong. Quote Link to comment Share on other sites More sharing options...
dustinnoe Posted May 29, 2007 Share Posted May 29, 2007 <?php $arr2 = serialize($arr); array_multisort ( $arr['tip_date'] , SORT_ASC, SORT_STRING , $arr['service_name'] , SORT_ASC, SORT_STRING , $arr['service_id'] , SORT_ASC, SORT_NUMERIC , $arr['tip_id'] , SORT_ASC, SORT_NUMERIC , $arr['tip_point'] , SORT_ASC, SORT_NUMERIC , $arr['tip_odds'] , SORT_ASC, SORT_NUMERIC , $arr['result'] , SORT_ASC, SORT_STRING , $arr['tip_text'] , SORT_ASC, SORT_STRING ); $arr2 = unserialize($arr2); ?> This will fix it but now the copy becomes the original and the original gets sorted. Quote Link to comment Share on other sites More sharing options...
per1os Posted May 29, 2007 Share Posted May 29, 2007 Really weird, cannot say that I ever really tried that. Maybe make an array copy function to avoid the reference? <?php function array_copy($array) { if (is_array($array)) { foreach ($array as $key => $val) { $newArray[$key] = $val; } } return $newArray; } ?> Unsure how it would work with a multi-dimm array, you made need it to be recursive to compensate for that, but maybe that would work? Quote Link to comment Share on other sites More sharing options...
dustinnoe Posted May 29, 2007 Share Posted May 29, 2007 Better yet... <?php $arr2 = $arr; $arr = serialize($arr2); array_multisort ( $arr2['tip_date'] , SORT_ASC, SORT_STRING , $arr2['service_name'] , SORT_ASC, SORT_STRING , $arr2['service_id'] , SORT_ASC, SORT_NUMERIC , $arr2['tip_id'] , SORT_ASC, SORT_NUMERIC , $arr2['tip_point'] , SORT_ASC, SORT_NUMERIC , $arr2['tip_odds'] , SORT_ASC, SORT_NUMERIC , $arr2['result'] , SORT_ASC, SORT_STRING , $arr2['tip_text'] , SORT_ASC, SORT_STRING ); $arr = unserialize($arr); ?> Now the original remains the same and the copy is sorted Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 29, 2007 Share Posted May 29, 2007 Read the user notes on the array_multisort() function. This problem is noted at least twice. One solution is the serialize() one, another is to modify the copy before calling array_multisort(). $arr2 = $arr; $arr2[0] = $arr2[0] array_multisort(/* $arr2 stuff here */); ^^^ Should work. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted May 30, 2007 Author Share Posted May 30, 2007 I love you all. PS don't normally get that far down a page in the manual - shall endeavour to make amends... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.