Web Hosting Talk







View Full Version : sorting an array in PHP


portalplanet
08-08-2005, 11:13 AM
I have something like this that I need to figure out how to sort:

111.23 store A
10.3 store b
15.76 store c
120.3 store d
5.7 store z

I need to sort them based on the first number, lowest one first. I'm new to working with arrays in PHP is that the best way to do it? Any help is appreciated.

Thanks!
Justin

portalplanet
08-08-2005, 04:06 PM
I guess I should add some more to clear up something.. I have this:

111.23 store A
10.3 store b
15.76 store c
120.3 store d
5.7 store z

The numbers are say the miles to that store, say store a. What I need to do is print out the array in order of closest to farthest in distance.

Thanks,
Justin

fastduke
08-08-2005, 04:46 PM
how is the array structured?

$list = array( '111.23' => 'store A',
'10.3' => 'store b' ); // Like this?

// or like this

$list = array( '111.23 store A', '10.3 store b');

portalplanet
08-08-2005, 04:56 PM
Actually it's a while loop that flows like this:

1. get users zip & query database for users latitude and longitude
2. mysql query to get all stores latitude and longitude
while ($stores) {
$mileage = getdistance ($lat_a, $long_a, $lat_b, $long_b);
right now I just print out the store name and distance. I need to somehow sort the output based on the closest store.
}

Justin

fastduke
08-08-2005, 07:21 PM
Hmm.. Having a hard time understanding your sample code but basically you want to build an associative array:

while ($stores) {
$mileage = getdistance ($lat_a, $long_a, $lat_b, $long_b);
$mixed{$mileage} = $store;
}


// Example of showing the difference in the array
echo "<pre>" . print_r($mixed, TRUE) . "</pre>";

ksort($mixed); //sort the array by key which is the mileage

echo "<pre>" . print_r($mixed, TRUE) . "</pre>";

Froggy
08-08-2005, 08:15 PM
Sorting some list of objects is just a better of using something like bubble sort of merge sort.

error404
08-09-2005, 08:08 PM
Write yourself a comparison function like:

function compare_locations($a, $b)
{
if ($a['miles'] > $b['miles'])
return 1;
else if ($a['miles'] < $b['miles'])
return -1;
else
return 0;
}


Make the conditions as complicated as you like. Then use the usort() function to sort your array:

usort($data_array, 'compare_locations');

deuce868
08-10-2005, 02:59 PM
while ($stores) {
$mileage = getdistance ($lat_a, $long_a, $lat_b, $long_b);
$results[$mileage] = $store_name; //not sure if $stores is the name or an array, etc
}

ksort($results);

foreach ($results as $k=>$v)
print $v . 'is ' . $k . ' miles away';

portalplanet
08-10-2005, 03:02 PM
Deuce,

What about 2 stores with the same mileage?

Justin

deuce868
08-10-2005, 04:43 PM
well then save it the other way and sort not by key

So change to:
while ($stores) {
$mileage = getdistance ($lat_a, $long_a, $lat_b, $long_b);
$results[$store_name] = $milage; //not sure if $stores is the name or an array, etc
}

sort($results);

foreach ($results as $k=>$v)
print $k . 'is ' . $v . ' miles away';

What do you want to do when the stores are the same distance? You can run the $results array through a function to check if the first elements share the same distance and just return once you get to a distance that is different.