1

I'm working in PHP CodeIgniter framework. Here I have an array with values distance field is in all the array. So I want to sort this array based on distance variable. when the distance is small it should be show at the top of the array.

Array
(   

    [3] => Array
        (

            [distance] => 0.1867701321678877
            [search_name] => gshshs
            [type] => vendor
        )

    [4] => Array
        (

            [distance] => 0.2581692930636528
            [search_name] => imarahtech
            [type] => vendor
        )

    [5] => Array
        (

            [distance] => 13.022316349008124
            [delivery_available] => Yes
            [search_name] => mius
            [type] => vendor
        )

    [6] => Array
        (

            [distance] => 23.49767368340837
            [search_name] => imarahtech
            [type] => vendor
        )

    [7] => Array
        (

            [distance] => 37.85888922426821
            [search_name] => blu
            [type] => vendor
        )


    [8] => Array
        (

            [distance] => 0.18766008198496642
            [search_name] => fried rice 
            [type] => product
        )

    [14] => Array
        (

            [distance] => 13.022316349008124
            [search_name] => meals rice
            [type] => product
        )

    [15] => Array
        (

            [distance] => 37.85888922426821
            [search_name] => ayla
            [type] => product
        )

)

so here what I wanted, Show the data Ascending order.

Output

Array
(   

    [3] => Array
        (

            [distance] => 0.1867701321678877
            [search_name] => gshshs
            [type] => vendor
        )

    [8] => Array
        (

            [distance] => 0.18766008198496642
            [search_name] => fried rice 
            [type] => product
        )

    [4] => Array
        (

            [distance] => 0.2581692930636528
            [search_name] => imarahtech
            [type] => vendor
        )

    [5] => Array
        (

            [distance] => 13.022316349008124
            [delivery_available] => Yes
            [search_name] => mius
            [type] => vendor
        )

    [14] => Array
        (

            [distance] => 13.022316349008124
            [search_name] => meals rice
            [type] => product
        )

    [6] => Array
        (

            [distance] => 23.49767368340837
            [search_name] => imarahtech
            [type] => vendor
        )

    [7] => Array
        (

            [distance] => 37.85888922426821
            [search_name] => blu
            [type] => vendor
        )

    [15] => Array
        (

            [distance] => 37.85888922426821
            [search_name] => ayla
            [type] => product
        )



)

Somebody Please help me to overcome this problem.

Thanking You

Muhammed Raheez PC
  • 393
  • 2
  • 4
  • 19

2 Answers2

1

try this

usort($arrData, function($a, $b)
{
    if ($a['distance'] == $b['distance'])   return 0;
    return ($a['distance'] > $b['distance']) ?   1   :   -1;
});

this is well documented here

Atural
  • 5,389
  • 5
  • 18
  • 35
0

In codeigniter you can do it easily in SQL by adding the row to your model

public function yourMethod(){

    $this->db->select();
    $this->db->from("your_table");
  //$this->db->where(...); //some condition, I guess

    $this->db->order_by('distance', "ASC");

    $query = $this->db->get();
    return $query->result();
}
Eiji
  • 454
  • 6
  • 15