Jump to content

multisort problem


ToonMariner

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/53428-multisort-problem/
Share on other sites

<?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.

Link to comment
https://forums.phpfreaks.com/topic/53428-multisort-problem/#findComment-264014
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/53428-multisort-problem/#findComment-264017
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/53428-multisort-problem/#findComment-264030
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/53428-multisort-problem/#findComment-264056
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.