【问题标题】:PHP combine multiple arrays into multidimensional arrayPHP将多个数组组合成多维数组
【发布时间】: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


【解决方案1】:

将主题标签用作数组中的键会更容易。你可以 如果需要,稍后将其转换为最终格式。这个想法是 遍历您的输入数组并在每个元素内迭代给定的 hashtags 字符串,增加计数器。

如果你的标签总是在这样的字符串中,用 空格,您还可以使用explode()preg_split() 进行更精细的控制。

$posts = # your input array
$tags = [];

foreach ($posts as $post) {
    $hashtags = explode(' ', $post['caption']);
    foreach ($hashtags as $tag) {
        if (!key_exists($tag, $tags)) {
            # first time seeing this one, initialize an entry
            $tags[$tag]['counter'] = 0;
            $tags[$tag]['likes'] = 0;
        }
        $tags[$tag]['counter']++;
        $tags[$tag]['likes'] += $post['likesCount'];
    }
}

转换为更接近您原始要求的内容:

$result = array_map(function($hashtag, $data) {
    return [
        'hash' => $hashtag,
        'times_used' => $data['counter'],
        'total_likes' => $data['likes'],
        'average_likes' => $data['likes'] / $data['counter'],
    ];
}, array_keys($tags), $tags);

【讨论】:

    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    相关资源
    最近更新 更多