Question

I have an array that contain user's activities on the website. It contains activities such as writing comments, news and groups. If two comments (or more) from different users have been written within an hour, I would like to gather those two arrays into one: User and 2 more commented on X. The code I have so far looks like this:

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

?>

So far the code above sorts the arrays by date (DESC). Desired result: one array: %s and 2 more commented in... and the other arrays removed from output. So by taking the latest comment and checking the date from the rest of the comments, it should be possible to handle this. I simply need some suggestions.

Thanks in advance

Was it helpful?

Solution

From what I understand from your question, I think you want to find out the number of users commenting in the last hour with respect to the latest commentor.

Using your logic, array_filter can help get those values which lie in the last hour.

This is the continuation of your code -

/*
...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.

Hope this helps-

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top