sort two arrays

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
ards
Forum Newbie
Posts: 3
Joined: Wed Apr 26, 2006 9:01 am

sort two arrays

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

Post 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)
ards
Forum Newbie
Posts: 3
Joined: Wed Apr 26, 2006 9:01 am

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply