使用PHP中的变量键对关联数组进行排序[关闭]
问题描述:
i have the following associative array
word => count
where word is an english word ('more', 'creme', 'car' and so on), while count is the number of times that word appeared in a given string.
I want to sort by count.
Thx.
Ps. I tried usort but didn't work
我有以下关联数组 p>
word => count p>
其中word是英文单词('more','creme','car'等),而count是单词出现在给定字符串中的次数。 p>
我想按计数排序。 p>
Thx。 p>
Ps。 我尝试过,但没有 工作 p> div>
答
<?php
$fruits = array("d" => 4, "a" => 3, "b" => 2, "c" => 1);
asort($fruits, SORT_NUMERIC);
foreach ($fruits as $key => $val) {
echo "$key = $val
";
}
?>