如何按字母顺序获取数组密钥



这是我的代码。我想以字母顺序获得 location_name location_id id id 想要相关的每个 location_name

模态

function get_exam_locations_for_view() {
  $d = $this->db->get('exm_exam_location');
  if ($d) {
    return $d;
  }
}

在视图中

foreach ($ex_locations->result_array() as $row) {
  $loc_name = $row['location_name'];
  $loc_id = $row['location_id'];
  $loc_ai = $row['id'];
}

您可以使用它基于键对数组进行排序。

$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);

,您也可以在查询本身中对结果进行排序。

function get_exam_locations_for_view() {
    $d = $this->db->select('exm_exam_location')->orderBy('exm_exam_location', 'asc')->get();
    if ($d) {
        return $d;
    }
}

最新更新