PHP,
pasted
on Feb 20:
|
<?php
$locations = array (
'phone' => array (
0 => '555-123-1234',
1 => '555-456-4321',
2 => '555-789-1122',
3 => '555-321-1111'
),
'address' => array (
0 => '123 Main Street BigCity, NY 12345',
1 => '456 Maple Street Somewhere, UT 87654',
2 => '999 1st Ave MyTown, VA 23456',
3 => '321 2nd Ave MyTown, VA 23456'
),
'distance' => array (
0 => 24.56,
1 => 13.05,
2 => 23.99,
3 => 1.75
),
'name' => array (
0 => 'First Location',
1 => 'Second Location',
2 => 'Third Location',
3 => 'Fourth Location'
)
);
$locationsAsRows = array();
for ($i = 0; isset($locations['phone'][$i]); $i++) {
$locationsAsRows[$i] = array (
'phone' => $locations['phone'][$i],
'address' => $locations['address'][$i],
'distance' => $locations['distance'][$i],
'name' => $locations['name'][$i]
);
}
echo "Before sorting:\n";
print_r($locationsAsRows);
array_multisort($locations['distance'], SORT_NUMERIC, $locationsAsRows);
echo "\n\nAfter sorting:\n";
print_r($locationsAsRows);
|
Output:
|
Before sorting:
Array
(
[0] => Array
(
[phone] => 555-123-1234
[address] => 123 Main Street BigCity, NY 12345
[distance] => 24.56
[name] => First Location
)
[1] => Array
(
[phone] => 555-456-4321
[address] => 456 Maple Street Somewhere, UT 87654
[distance] => 13.05
[name] => Second Location
)
[2] => Array
(
[phone] => 555-789-1122
[address] => 999 1st Ave MyTown, VA 23456
[distance] => 23.99
[name] => Third Location
)
[3] => Array
(
[phone] => 555-321-1111
[address] => 321 2nd Ave MyTown, VA 23456
[distance] => 1.75
[name] => Fourth Location
)
)
After sorting:
Array
(
[0] => Array
(
[phone] => 555-321-1111
[address] => 321 2nd Ave MyTown, VA 23456
[distance] => 1.75
[name] => Fourth Location
)
[1] => Array
(
[phone] => 555-456-4321
[address] => 456 Maple Street Somewhere, UT 87654
[distance] => 13.05
[name] => Second Location
)
[2] => Array
(
[phone] => 555-789-1122
[address] => 999 1st Ave MyTown, VA 23456
[distance] => 23.99
[name] => Third Location
)
[3] => Array
(
[phone] => 555-123-1234
[address] => 123 Main Street BigCity, NY 12345
[distance] => 24.56
[name] => First Location
)
)
|
|