【发布时间】:2017-05-23 16:44:13
【问题描述】:
我正在用 PHP 编写代码来收集我在所有媒体帖子中使用的所有主题标签,并查看我使用该主题标签的帖子有多少,以及总共收到多少喜欢带有该主题标签的帖子。
我已收集数据库中的所有媒体帖子,现在可以导出这些信息。这是一个正在输出的多维数组的示例:
Array
(
[0] => Array
(
[id] => 1
[caption] => #londra #london #london_only #toplondonphoto #visitlondon #timeoutlondon #londres #london4all #thisislondon #mysecretlondon #awesomepix #passionpassport #shootermag #discoverearth #moodygrams #agameoftones #neverstopexploring #beautifuldestinations #artofvisuals #roamtheplanet #jaw_dropping_shots #fantastic_earth #visualsoflife #bdteam #nakedplanet #ourplanetdaily #earthfocus #awesome_earthpix #exploretocreate #londoneye
[likesCount] => 522
)
[1] => Array
(
[id] => 2
[caption] => #londra #london #london_only #toplondonphoto #visitlondon #timeoutlondon #londres #london4all #thisislondon #mysecretlondon #awesomepix #passionpassport #shootermag #discoverearth #moodygrams #agameoftones #neverstopexploring #beautifuldestinations #artofvisuals #roamtheplanet #jaw_dropping_shots #fantastic_earth #visualsoflife #bdteam #nakedplanet #ourplanetdaily #earthfocus #awesome_earthpix #harrods #LDN4ALL_One4All
[likesCount] => 1412
)
)
我可以使用以下函数将这些主题标签分开:
function getHashtags($string) {
$hashtags= FALSE;
preg_match_all("/(#\w+)/u", $string, $matches);
if ($matches) {
$hashtagsArray = array_count_values($matches[0]);
$hashtags = array_keys($hashtagsArray);
}
return $hashtags;
}
现在我想为每个主题标签创建一个多维数组,如下所示:
Array
(
[0] => Array
(
[hash] => #londra
[times_used] => 2
[total_likes] => 153
)
[1] => Array
(
[hash] => #london
[times_used] => 12
[total_likes] => 195
)
)
我对此很陌生,不知道如何实现这一点。感谢您的帮助和建议!
【问题讨论】:
-
如何计算每个主题标签的点赞数?
-
这很难计算,但我会将使用has标签的帖子中的总赞数相加,然后除以它已使用的帖子数量。跨度>
标签: php arrays multidimensional-array push