2012-02-03 90 views
0

在下表中,$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>"; 
+2

你在寻找['asort()'](http://us.php.net/manual/en/function.asort.php)? – 2012-02-03 00:43:52

+0

使用'asort()'按降序排列,'arsort()'按升序排列 – 2012-02-03 00:49:09

+0

我在哪里放置'asort()'? – John 2012-02-03 01:10:56

回答

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); 
+0

我会在哪里放? – John 2012-02-03 01:13:28

+0

'echo'

';' – elimirks2012-02-03 15:38:26

1

后,这条线把asort()

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

这样按'$ word'升序排列表格行。我想按'$ count1'降序排序。 – John 2012-02-03 01:20:41

0

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

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

    arsort($result); 

    }