Forum Moderators: coopster

Message Too Old, No Replies

changing order of an array

returning values in a multidimensional array in a particular order

         

russkern

1:29 am on Jan 18, 2010 (gmt 0)

10+ Year Member



Hello all,

I have a little need that is stumping me, I'm hoping someone can help.

I have a group of ZIP codes (zipcodesB) that are stored in an array, as well as a script that will tell me how far each of them is from ZipcodeA.

I want to write them back into a Multi-dimensional array with zipcode & distance like so:

$zips=array(array(44720,0),array(44721,0),array(44735,3), etc.)

How would I go about writing each one to screen (or perform some other function) based on distance from closest to farthest?

any help would be appreciated.

R

script3r

3:30 am on Jan 18, 2010 (gmt 0)

10+ Year Member



Well you could sort the resultant $zips array using a supplied function together with usort.

i.e.:


function compareByDistance( $z1, $z2 ) {
//assuming zip[0] == code && zip[1] == distance
return ( $z1[1] < $z2[1] ) ? -1 : 1;
}

usort( $zips, "compareByDistance" );

foreach( $zips as $zip ) {
echo ' ... ';
}