使用JSON捕获推文并按喜欢排序?

问题描述 投票:0回答:1

我目前正在运行wordpress后端,并希望在我的网站上显示一些基于hastags的推文。对于常规API请求和数据库存储,我使用此函数:

private function parseRequest($json) {
    $tmp = $json;
    $result = array();

    if (isset($json['statuses'])) {
        $tmp = $json['statuses'];
    }
    if (isset($tmp) && is_array($tmp)){
        foreach ($tmp as $t) {
            $this->image = null;
            $this->media = null;
            $tc = new \stdClass();
            $tc->feed_id = $this->id();
            $tc->id = $t['id_str'];
            $tc->type = $this->getType();
            $tc->nickname = '@'.$t['user']['screen_name'];
            $tc->screenname = (string)$t['user']['name'];
            $tc->userpic = str_replace('.jpg', '_200x200.jpg', str_replace('_normal', '', (string)$t['user']['profile_image_url']));
            $tc->system_timestamp = strtotime($t['created_at']);
            $tc->text = $this->getText($t);
            $tc->userlink = 'https://twitter.com/'.$t['user']['screen_name'];
            $tc->permalink = $tc->userlink . '/status/' . $tc->id;
            $tc->media = $this->getMedia($t);
            @$tc->additional = array('shares' => (string)$t['retweet_count'], 'likes' => (string)$t['favorite_count'], 'comments' => (string)$t['reply_count']);
            if ($this->isSuitablePost($tc)) $result[$tc->id] = $tc;
        }
    }
    return $result;
}

现在我正在寻找一个函数来计算“附加数组中的所有变量,例如共享+喜欢+评论,并根据得到的数字对所有帖子进行排序。我使用标准的wordpress sql数据库。我找不到解决方案或者我我只是失明

谢谢你的问候

php json wordpress twitter
1个回答
0
投票

你可以使用一个简单的usort函数:

usort($tc, function($a, $b) {

    $a_sum = array_sum($a->additional);
    $b_sum = array_sum($b->additional);

    if ($a_sum == $b_sum) {
        return 0;
    }

    return ($a_sum < $b_sum) ? -1 : 1;
});
© www.soinside.com 2019 - 2024. All rights reserved.