我有一个包含状态的数组,包含键值对,其中键是公司。
我想为每个州排序数组,以便首先列出价值最高的公司,最后列出价值最低的公司。
array(
[Uttar Pradesh] => Array
(
[Noida Power Company Ltd] => 111.83
[Uttar Pradesh Power Corporation Ltd] => 74.51
[Anpara-C TPS] => 72.73
[Matatila HPS (UPJVUN)] => 1.88
[Rihand Hydro Power Station] => 3.57
[Unchahar Solar] => 2.15
)
[Odisha] => Array
(
[Grid Company of Orissa] => 57.2
[Sail Rourkela Steel Plant] => 1.03
[GMR Kamalanga Energy Ltd.] => 271.62
[Jindal Stainless Ltd, Orissa] => 63.78
[Sterlite Energy Limited] => 209.48
[Talcher Solar] => 1.69
)
)
我希望输出数组为
array(
[Uttar Pradesh] => Array
(
[Noida Power Company Ltd] => 111.83
[Uttar Pradesh Power Corporation Ltd] => 74.51
[Anpara-C TPS] => 72.73
[Rihand Hydro Power Station] => 3.57
[Unchahar Solar] => 2.15
[Matatila HPS (UPJVUN)] => 1.88
)
[Odisha] => Array
(
[GMR Kamalanga Energy Ltd.] => 271.62
[Sterlite Energy Limited] => 209.48
[Jindal Stainless Ltd, Orissa] => 63.78
[Grid Company of Orissa] => 57.2
[Talcher Solar] => 1.69
[Sail Rourkela Steel Plant] => 1.03
)
)
我在网上搜索但无法回答我的要求,请帮助我。
答案 0 :(得分:1)
循环遍历数组并使用arsort通过按降序排序值来制作数组。
$array = array(
"Uttar Pradesh" => array
(
"Noida Power Company Ltd" => 111.83,
"Uttar Pradesh Power Corporation Ltd" => 74.51,
"Anpara-C TPS" => 72.73,
"Matatila HPS (UPJVUN)" => 1.88,
"Rihand Hydro Power Station" => 3.57,
"Unchahar Solar" => 2.15
),
"Odisha" => array
(
"Grid Company of Orissa" => 57.2,
"Sail Rourkela Steel Plant" => 1.03,
"GMR Kamalanga Energy Ltd." => 271.62,
"Jindal Stainless Ltd, Orissa" => 63.78,
"Sterlite Energy Limited" => 209.48,
"Talcher Solar" => 1.69
)
);
foreach ($array as $key => &$value) {
arsort($value);
}
echo "<pre>";
print_r($array);
echo "</pre>";
礼貌:@u_mulder在评论中