Forum Moderators: coopster

Message Too Old, No Replies

reciprocal sorting of arrays

         

sssweb

4:29 pm on Aug 12, 2006 (gmt 0)

10+ Year Member



I have 2 arrays that I want to keep sequentially in sync after one has been re-sorted. E.G.:

$array1 = array(1, 2, 3, 4, 5);
$array2 = array(a, b, c, d, e);

[code acts on $array1 so that it becomes]:

$array1 = array(3, 1, 5, 4, 2);

I need code that gives this result:

$array2(c, a, e, d, b);

This is probably pretty simple using the keys for each, but I'm not skilled enough in php to do it.

sssweb

4:44 pm on Aug 12, 2006 (gmt 0)

10+ Year Member



I just realized this may be trickier than I thought. The code that 're-sorts' $array1 is SQL (i.e. it is a field of data values that re-sorts using SQL 'ORDER BY').

I'm not sure then how I would retain the original keys in $array1 so I could apply them to $array2.

jatar_k

12:28 am on Aug 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



drop them into a multi dimensional array maybe

maybe using something like this

$array3 = array_combine [php.net]($array1,$array2);

then you would just need to sort it

array_combine is PHP5 only, I think you could loop through them and do the same thing if need be.

You could try using something like array_multisort [php.net] as well

sssweb

4:53 pm on Aug 13, 2006 (gmt 0)

10+ Year Member



Thanks...I've found a solution for this.