热门Hashtags

时间:2016-01-12 18:02:40

标签: php mysql

好的,这是我到目前为止所拥有的;基本上它选择最流行的哈希标签并在DESC中回显它们但是当它回显它们时它会打印出来,如下所示我如何改变它;

回应的内容 这是页面上显示的内容

Array ( [#conversation] => 1 [#new] => 1 )

选择语句如何让它回显哈希标记。

<?php

$result = mysql_query("SELECT subject, count(usr_users.subject)as subjectCount FROM usr_users 
    GROUP BY usr_users.subject
    ORDER BY subjectCount DESC
    ")or die(mysql_error());
$maxLines = 10;
while ($row = mysql_fetch_array($result)) {
    $str1 = $row['subject'];
    $a = explode(' ', $str1);
    $size = sizeof($a);
    for ($i = 0; $i < $size; $i++) {
        $v = $a[$i];
        if ($v[0] == '#') {
            $hash_arr[] = $v;
        }
    }
    if (0 == $maxLines)
        break;
}
$t = array_count_values($hash_arr);
ksort($t);
print_r($t);
?>

2 个答案:

答案 0 :(得分:0)

试试这个

 <?php 
$result = mysql_query("SELECT subject, count(usr_users.subject)as subjectCount FROM usr_users 
GROUP BY usr_users.subject
ORDER BY subjectCount DESC
")or die(mysql_error());
$maxLines = 10;
while($row = mysql_fetch_array( $result )) 
{ 
$str1= $row['subject'];
$a=explode(' ',$str1);
$size=sizeof($a);
for($i=0;$i<$size;$i++){
$v=$a[$i];
if($v[0]=='#'){
$hash_arr[]=$v;
}
}
if(0==$maxLines)
break;
}
$t=array_count_values($hash_arr);
ksort($t);
//print_r($t);
while (list($key, $val) = each($t)) {
                if(!empty($val)){
                    echo $fields= $key ."count is->";
                    echo $valset=  $val ;// if you want to comment this value then comment it .
                    echo "<br>";
                }
            }
// or
//foreach($t as $row=>$value ){echo $row."<br>";}
?>

或使用foreach of veile

foreach($t as $row=>$value ){
echo $row."<br>";
}

如果你想用一个值替换#使用

foreach($t as $row=>$value ){

echo  str_replace('#','',$row) ;
echo "</br>";

}

结果:

  conversation
  new

答案 1 :(得分:0)

print_r并没有按照您的想法行事。您希望迭代数组以使用foreach。演示:

$array = array("#conversation", "#new");
$t=array_count_values($array);
foreach($t as $hash => $count) {
    echo $hash . "\n";
}

输出:

  

#conversation
  #NEW

演示:https://eval.in/501267