[英]php error - array_multisort
突然我的索引頁面上顯示此錯誤,根本沒有任何變化,並且已經工作了幾個月,
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>";
?>
該錯誤可能是由於數據庫中沒有數據。 $row['hashtags']
為空。 嘗試直接在sql服務器上執行查詢,看看是否有任何結果。
更新 :我更新了您的代碼以處理錯誤:
<?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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.