php错误 - array_multisort

时间:2011-12-25 10:16:40

标签: php

突然间我在索引页面显示此错误显示根本没有发生任何变化,并且工作了几个月,

PS:我的直播网站:sudanesetweeps.com

array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in

Warning: Invalid argument supplied for foreach() in

我正在使用以下代码:

<?php

require_once("dbconnect.php");
$query = " SELECT COUNT( * ) cnt, hashtags
FROM  tweets
WHERE tweeted_at > DATE_SUB( NOW( ) , INTERVAL 1 DAY ) 
AND hashtags !=  '' 
GROUP BY hashtags
ORDER BY cnt DESC ";
$res = mysql_query($query);
while($row = mysql_fetch_assoc($res) ) {
 $count = $row['cnt'];
$hashtags = explode( " ", $row['hashtags'] );
foreach($hashtags as $hashtag ) {   
 //$index = array_search($hashtag, array_keys($topics));
    if( strtolower($hashtag) != 'gmaes' && strtolower($hashtag) != 'lord'      ) 
        $topics[strtolower($hashtag)] += $count;
  }
 }

 array_multisort($topics, SORT_DESC);

 echo "<ul id='mytags'>";
 $index = 0;
  foreach($topics as $key=>$value) {
$index++;
if($key != "" ) {       
            $trending[$key] = $value;
            echo "<li><a class='size".$index."'      href='http://twitter.com/#!/search/realtime/%23".$key."'>#".$key."</a></li>";
   }
  if($index >6 ) 
  break;    
}
 echo "</ul>";

 ?>

1 个答案:

答案 0 :(得分:-1)

错误可能是由于数据库中没有数据。 $row['hashtags']是空的。尝试直接在sql server上执行查询,看看是否有任何结果。

更新:我更新了您的代码以处理错误:

<?php

require_once("dbconnect.php");
$query = " SELECT COUNT( * ) cnt, hashtags
FROM  tweets
WHERE tweeted_at > DATE_SUB( NOW( ) , INTERVAL 1 DAY ) 
AND hashtags !=  '' 
GROUP BY hashtags
ORDER BY cnt DESC ";
$res = mysql_query($query);
$topics = array();
while($row = mysql_fetch_assoc($res) ) {
    $count = $row['cnt'];
    $hashtags = explode( " ", $row['hashtags'] );
    if(!empty($hashtags))
    {
        foreach($hashtags as $hashtag ) {
            //$index = array_search($hashtag, array_keys($topics));
            if( strtolower($hashtag) != 'gmaes' && strtolower($hashtag) != 'lord') 
                $topics[strtolower($hashtag)] += $count;
        }
    }
}
if(!empty($topics))
{
    array_multisort($topics, SORT_DESC);

    echo "<ul id='mytags'>";
    $index = 0;
    foreach($topics as $key=>$value) {
        $index++;
        if($key != "" ) {
            $trending[$key] = $value;
            echo "<li><a class='size".$index."'      href='http://twitter.com/#!/search/realtime/%23".$key."'>#".$key."</a></li>";
        }
        if($index >6 )
            break;
    }
}