【问题标题】:php array sorting in order [duplicate]php数组按顺序排序[重复]
【发布时间】:2012-07-06 18:55:18
【问题描述】:

可能重复:
Sorting an associative array in PHP
Sorting a multidimensional array in PHP?

我有一个系统允许将订单分配给送货司机。以下来源列出了当前可用的分销商列表,以及他们与客户的距离:

$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.')');
                 }

列表使用下拉菜单显示:

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

我需要按照最短距离到最高距离的顺序对数组进行排序。

【问题讨论】:

标签: php arrays sorting


【解决方案1】:
  1. 将距离添加到$franchise_a_status
  2. 按距离排序 (usort)。
  3. 创建一个没有距离值的新数组(array_mapforeach)。

【讨论】:

  • 请您再详细说明一下。 $distance 包含在 $franchise_a_status 数组中。
  • 我认为最好把它作为它自己的值,用它自己的键。
猜你喜欢
  • 2015-04-29
  • 1970-01-01
  • 2011-08-31
  • 2011-08-03
  • 1970-01-01
  • 2013-03-18
  • 1970-01-01
  • 2017-08-01
  • 2019-08-20
相关资源
最近更新 更多