doudun1029 2017-05-23 16:44
浏览 150
已采纳

PHP将多个数组组合成多维数组

I am writing code in PHP to collect all the hashtags which I've used in all my media posts and see in how many posts I've used the hashtag and how many likes the post with that hashtag received in total.

I have collected all of the media posts in my database and are now able to export this information. Here is an example of the multidimensional array which is being output:

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
        )
)

I am able to separate these hashtags out using the following function:

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;
}

Now I want to create a multidimensional array for each hashtag which should look like this:

Array
(
    [0] => Array
    (
        [hash] => #londra
        [times_used] => 2
        [total_likes] => 153
    )
    [1] => Array
    (
        [hash] => #london
        [times_used] => 12
        [total_likes] => 195
    )
)

I am quite new to this and not sure how to achieve this. Help and suggestions are appreciated!

  • 写回答

1条回答 默认 最新

  • doulian4467 2017-05-23 18:29
    关注

    It would be easier to use the hashtags as keys in your array. You can transform it later to your final format if you want to. The idea is to traverse your input array and within each element iterate on the given hashtags string, increasing counters.

    And if your hashtags are always in a string like that, separated by whitespace, you can also get an array of then with explode() or preg_split() for finer control.

    $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'];
        }
    }
    

    Transforming to something closer to your original request:

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)