2014-02-23 4 views
1

웹 사이트에서 사용자의 활동이 포함 된 배열이 있습니다. 여기에는 의견, 뉴스 및 그룹 작성과 같은 활동이 포함됩니다. 1 시간 내에 서로 다른 사용자의 의견 (두 개 이상)이 작성된 경우이 두 배열을 하나씩 수집하고 싶습니다. 사용자와 2 명이 X에 댓글을 달았습니다.시간 범위 내에서 배열 요소를 병합 - 어떻게?

<?php 

$output = array(); 
$output[] = array('userID' => 12, 'txt' => sprintf('%s commented in %s', 'User1', 'Event'), 'date' => 1393080072); 
$output[] = array('userID' => 13, 'txt' => sprintf('%s commented in %s', 'User2', 'Event'), 'date' => 1393080076); 
$output[] = array('userID' => 13, 'txt' => sprintf('%s created the news %s', 'User2', 'RANDOMNEWS'), 'date' => 1393080080); 
$output[] = array('userID' => 14, 'txt' => sprintf('%s commented in %s', 'User3', 'Event'), 'date' => 1393080088); 

$date = array(); 
foreach($output as $k => $d) { 
    $date[$k] = $d['date']; 
} 

array_multisort($date, SORT_DESC, $output); 

print_r($output); 

?> 

위 코드는 날짜별로 배열을 정렬합니다 (DESC). 원하는 결과 : 하나의 배열 : % s 및 2 개가 ...에 주석 처리되었으며 다른 배열은 출력에서 ​​제거되었습니다. 따라서 최신 의견을 받고 나머지 의견에서 날짜를 확인하면이를 처리 할 수 ​​있어야합니다. 나는 단지 약간의 제안이 필요하다. 나는 당신의 질문에서 이해하는 바로는 사전

+0

누구? 더 많은 정보가 필요하면 언제든지 물어보십시오. – hskrijelj

답변

0

에서

덕분에, 나는 당신이 최신 commentor에 대한 마지막 시간에 주석 사용자의 수를 알아 보려는 생각합니다.

로직을 사용하면 array_filter은 지난 1 시간 동안 값을 얻을 수 있습니다.

이 코드의 연속이다 -

/* 
...your code... 
*/ 

$latest_time = $output[0]['date']; 
$hour_past_time = $latest_time - 3600; 
$user_ids = Array(); 
$res=array_values(
       array_filter($output,function($arr)use($latest_time, $hour_past_time,&$user_ids){ 
          if(
           $arr["date"] <= $latest_time && 
           $arr["date"] >= $hour_past_time && 
           in_array($arr['userID'],$user_ids) == false 
          ){ 
           $user_ids[] = $arr['userID']; 
           return true; 
          } 
         } 
       ) 
); 
echo "Users with their latest comments in the past hour- <br />"; 
var_dump($res); 
$latest_user_id = "User".$res[0]['userID']; 
$rest = count($res) - 1; 
echo "<br />$latest_user_id and $rest more commented.<br />"; 

OUTPUT -

Users with their latest comments in the past hour- 
array 
    0 => 
    array 
     'userID' => int 14 
     'txt' => string 'User3 commented in Event' (length=24) 
     'date' => int 1393080088 
    1 => 
    array 
     'userID' => int 13 
     'txt' => string 'User2 created the news RANDOMNEWS' (length=33) 
     'date' => int 1393080080 
    2 => 
    array 
     'userID' => int 12 
     'txt' => string 'User1 commented in Event' (length=24) 
     'date' => int 1393080072 

User14 and 2 more commented. 

희망이 helps-

+0

정확하게 필요한 답변을 주셔서 감사합니다. – hskrijelj

관련 문제