search

Home  >  Q&A  >  body text

php - Sort associative array

Array
id Name Age Language
1 Novel 16 90
2 Xiaomeng 18 93
2 Kitten 17 92
Question:
This is a table.
1: How to sort by age from smallest to largest?
2: How to sort by Chinese language from smallest to largest?
3: ID sorting is the default, so don’t use it╮( ̄▽ ̄"")╭
Simply put, the associative array is sorted by a certain key.

天蓬老师天蓬老师2789 days ago630

reply all(2)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:16:01

    arr.sort((a, b)=> { return a.age - b.age })

    Other similar

    reply
    0
  • 高洛峰

    高洛峰2017-05-16 13:16:01

    Learn about this function: array_multisort()

    function arr_sort($arr,$key_name){
            $sort = array();
            foreach ($arr as $key => $value)
            {
                $sort[$key] = $value["$key_name"];
            }
            array_multisort($sort, SORT_ASC , $arr);
    
            return $arr;
        }

    reply
    0
  • Cancelreply