Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Possible Duplicate:
Sorting an associative array in PHP
Sorting a multidimensional array in PHP?

I have a system which allows orders to be allocated to a delivery driver.The following source brings up a list of current available distributors, and the distance they are away from the customer:

$franchise_a_status = array();
  $franchise_a_status[] = array('id' => '', 'text' => 'Please Select');
  $franchise_query = mysql_query("select franchise_id, franchise_surname, franchise_firstname, franchise_postcode, franchise_availability  from " . TABLE_FRANCHISE . " where franchise_availability=1");
    $origin = $cInfo->entry_postcode;
  while ($franchise = mysql_fetch_array($franchise_query)) {
    $destination = $franchise['franchise_postcode'];
    $json = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$origin&destinations=$destination&mode=driving&sensor=false&units=imperial");
    $result = json_decode($json, true);
    $distance = $result['rows'][0]['elements'][0]['distance']['text'];


    $franchise_a_status[] = array('id' => $franchise['franchise_id'],
                                'text' => $franchise['franchise_surname']. ' ' .$franchise['franchise_firstname'].' '.'('.$distance.')');
                 }

the list is shown using a drop down menu:

    array('text' => tep_draw_pull_down_menu('franchise_id',$franchise_a_status));   

I need the array to be sorted in order of the shortest distance to the highest distance.

share|improve this question

marked as duplicate by hakre, PeeHaa, vascowhite, kapa, joran Jul 7 '12 at 3:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
How about sorting them using MySQL instead? –  Havelock Jul 6 '12 at 10:07
    
Look into using usort. PHP Documentation of usort –  Spiritfyre Jul 6 '12 at 10:08

1 Answer 1

  1. Add the distance to $franchise_a_status.
  2. Sort according to distance (usort).
  3. Create a new array without the distance value (array_map or foreach).
share|improve this answer
    
Can u expand on this a bit more please. the $distance is included in the $franchise_a_status array. –  user1506375 Jul 6 '12 at 10:23
    
I think it's best to include it as its own value, with its own key. –  Emil Vikström Jul 6 '12 at 11:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.