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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.