Dav*_*vid 1 php arrays sorting text
我有一个数组
print_r($RL_all_years_unique_0);
Run Code Online (Sandbox Code Playgroud)
给出:
Array ( [0] => kWh Electricity [1] => Kg Refrigerants [2] => Gallons Diesel [3] => Indirect VMT [4] => Short Tons Solid Waste [5] => Gallons Propane [6] => Cubic Ft Nat Gas [7] => Gallons 87 Gasoline [8] => WasteWater - kg N [9] => WasteWater - Pop )
Run Code Online (Sandbox Code Playgroud)
然后我做:
asort($RL_all_years_unique_0);
print_r($RL_all_years_unique_0);
Run Code Online (Sandbox Code Playgroud)
给出:
Array ( [6] => Cubic Ft Nat Gas [7] => Gallons 87 Gasoline [2] => Gallons Diesel [5] => Gallons Propane [3] => Indirect VMT [1] => Kg Refrigerants [4] => Short Tons Solid Waste [9] => WasteWater - Pop [8] => WasteWater - kg N [0] => kWh Electricity )
Run Code Online (Sandbox Code Playgroud)
为什么 kWh Electricity 是最后,而不是之后"Kg Refrigerants"?除了那个之外,一切都是按字母顺序排列的。
使用natcasesort进行不区分大小写的排序。
natcasesort($RL_all_years_unique_0);
print_r($RL_all_years_unique_0);
Run Code Online (Sandbox Code Playgroud)
或者
sort($fruits, SORT_NATURAL | SORT_FLAG_CASE);
Run Code Online (Sandbox Code Playgroud)
或者
asort($fruits, SORT_NATURAL | SORT_FLAG_CASE);
Run Code Online (Sandbox Code Playgroud)