2017-05-23 4 views
1

PHP로 코드를 작성하여 모든 미디어 게시물에 사용한 해시 태그를 수집하고 해시 태그를 사용한 게시물 수와 얼마나 많은 게시물을 좋아하는지 확인합니다. 해시 태그가 총 수신되었습니다.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 
    ) 
) 

저는 이것을 아주 잘 알고 있으며이를 달성하는 방법을 모르겠습니다. 도움과 제안을 부탁드립니다!

+3

어떻게 당신이 좋아하는 각 해시 태그에 대한 계산 계산합니까? – sidyll

+0

계산하기는 어렵지만 has 태그가 사용 된 게시물의 전체 좋아요 수를 요약 한 다음 사용 된 게시물 수로 나눕니다. –

답변

0

해시 태그를 배열의 키로 사용하는 것이 더 쉽습니다. 원하는 경우 나중에 을 최종 형식으로 변환 할 수 있습니다. 아이디어는 입력 배열을 가로 지르고 각 요소 내에서 지정된 해시 태그 문자열을 반복하면서 카운터를 증가시키는 것입니다.

그리고 당신의 해시 태그가 공백으로 구분 같은 문자열에 항상있는 경우, 당신은 또한 정밀한 제어를위한 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); 
+0

놀라운 @sidyll! 그건 잘된거야! –

관련 문제