0

This is my code. I want to get location_name in alphabetical order and location_id and id want to relevant each location_name

in modal

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

in view

foreach ($ex_locations->result_array() as $row) {
  $loc_name = $row['location_name'];
  $loc_id = $row['location_id'];
  $loc_ai = $row['id'];
}
DavidDomain
  • 14,976
  • 4
  • 42
  • 50
samrulz ss
  • 43
  • 6
  • check this: https://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php – Jigar Shah Jun 28 '17 at 07:18

1 Answers1

1

You can use this to sort array based on key.

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

ksort($fruits);

and You can also sort the results in query itself.

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

    if ($d) {
        return $d;
    }
}
Vasu Kuncham
  • 528
  • 3
  • 14