按数组结果对表进行排序

时间:2012-02-03 00:42:20

标签: php

在下表中,$count1是一个数值。如何按$count1降序对表进行排序?

        $words = explode(" ", $commentstring);
        $result = array_combine($words, array_fill(0, count($words), 0));

        foreach($words as $word) {
        $result[$word]++;
        }


        echo "<table>";

        foreach($result as $word => $count1) {

        echo '<tr>';    
        echo '<td>';
        echo "$word";
        echo '</td>';

        echo '<td>';
        echo "$count1 ";
        echo '</td>';

        echo '</tr>';

        }

        echo "</table>";

3 个答案:

答案 0 :(得分:1)

你也可以使用sort($array_var, SORT_DESC); http://us.php.net/manual/en/function.sort.php

编辑:

用法

$foo = array('bar', 'car', 'apple', 'food', 'banana');
sort($foo, SORT_DESC);

答案 1 :(得分:1)

此行后asort()

 $words = explode(" ", $commentstring);
 asort($words);

答案 2 :(得分:0)

感谢响应者......他们引导我朝着正确的方向前进。我终于得到了这个工作:

foreach($words as $word) {
    $result[$word]++;

    arsort($result);

    }