Page 1 of 1

sort two arrays

Posted: Wed Apr 26, 2006 10:38 pm
by ards
have two arrays for eg
$arr1 = (1,2,4,3,5);
$arr2 = (10,2,6,19,21);

these are related, ie arr1[x] corresponds to arr2[x]..... they are x and y axis values for a graph.
the problem i have is that my x axis array is not sorted. i am drawing a line graph so i need it sorted.

sort($arr1) works like a charm!
but i want to keep the values linked. is there some type of way to simply achieve this. can i put both arrays into 1 big associative array or something?

i want to end up with
$arr1 = (1,2,3,4,5);
$arr2 = (10,2,19,6,21);........... so only swap values in arr2 that have be swapped in arr1

thanks in advance
-ards

Posted: Wed Apr 26, 2006 11:04 pm
by feyd
array_combine() would fit the bill nicely. If you're using php4, there are several php4 implementations available (just look through the user comments)

Posted: Thu Apr 27, 2006 12:46 am
by ards
thats a great ftn, thanks feyd

my problem lies now in the fact that some of the x values are duplicates, so the x axis array is more like $arr1 = (60,60,60,61,60,62,62,63,64,65,66) etc
and the array_combine function only takes the last corresponding valeu in the y axis array

i assume because of the properties of an array that you could not have two elements with the same index.

this one has me stumped

Posted: Thu Apr 27, 2006 12:56 am
by s.dot
why not merge your two arrays into one large array with keys => values being xaxis => yaxis

Code: Select all

$array = ('xaxis' => 'yaxis', 'xaxis2' => 'yaxis2');
then run some sort of array sorting function that maintains keys, so the values will never be split up.

... hope I made sense there :P

Edit: Nevermind, that's what the above function does.