按流行度排序标签数组

时间:2015-01-20 12:29:10

标签: php arrays json sorting

我正在加载一系列标签,并希望按人气排序,其中应首先列出最受欢迎的单词,依此类推。 怎么做?

PHP:

function addTags($jsonArray)
{
    $output = array();
    foreach($jsonArray['response']['posts'] as $post)
    {
        foreach($post['tags'] as $tag)
        {
                $output[] = $tag;
        }
    }
    return $output;
}

JSON标记示例:

["mostpopular","single","mostpopular","foo","foo","mostpopular","bar"]

1 个答案:

答案 0 :(得分:0)

检查这有帮助

$jsonArray = array ("mostpopular","single","mostpopular","foo","foo","mostpopular","bar");

$jsonresult = array_count_values($jsonArray);

echo '<pre>'; print_r($jsonresult);echo '</pre>';
//Array (  [mostpopular] => 3 [single] => 1    [foo] => 2    [bar] => 1)    
$jsonsort = arsort($jsonresult);
echo '<pre>'; print_r($jsonresult);echo '</pre>';
// Array ([mostpopular] => 3 [foo] => 2 [single] => 1        [bar] => 1)