0

I have a multidimensional array that I get from DB. Array has a number of views by each hour that is logged in the DB as a view, and it looks like this:

array:11 [▼
    0 => array:2 [▼
         "hour" => 0
         "views" => 1
         ]
    1 => array:2 [▼
         "hour" => 1
         "views" => 1
         ]
    2 => array:2 [▼
         "hour" => 4
         "views" => 1
         ]
    ...and so on
    ]

I need to make a new array that will contain number of views for range of 2 hours. So for example from the array shown above I would like to get an array with, number of views for time between 0-2, that would 2, and for 2-4, would be 0 in this case, and so on.

2
  • Why and for 2-4, would be 0 if 2 => array:2 [▼ "hour" => 4 "views" => 1 Commented May 10, 2016 at 9:07
  • Because that view would be counted for range 4-6, and since the views for 2 and 3 are missing, I need to put 0 for that range Commented May 10, 2016 at 9:12

3 Answers 3

1

You can do it in Mysql query:

select floor(hour/2) range, sum(views) sum 
   from thetable 
 group by range
Sign up to request clarification or add additional context in comments.

6 Comments

Do you know maybe how would I do that query in laravel preferably, my table from which I am pulling those views, has a column created_at which is a timestamp that looks like this: 2016-05-03 22:41:59
how do you receive "hour" => 0 from timestamp ?
I have this kind of query now: View::groupBy('hour') ->orderBy('hour', 'ASC') ->get([ DB::raw('Hour(created_at) as hour'), DB::raw('COUNT(*) as "views"') ])->toArray();
i never saw laravel. but think it will be View::groupBy('range') ->orderBy('range', 'ASC') ->get([ DB::raw('floor(Hour(created_at)) as range'), DB::raw('COUNT(*) as "views"') ])->toArray();
Thanks, I think these is probably the best solution to do everything in the query. Thanks!
|
1

You can just use a foreach to create a new array.

<?php


$your_array = [0 => [
         "hour" => 0,
         "views" => 4
         ],
    1 => [
         "hour" => 1,
         "views" => 12
         ],
    2 => [
         "hour" => 4,
         "views" => 1
         ],
    3 => [
         "hour" => 2,
         "views" => 9
         ],
    4 => [
         "hour" => 21,
         "views" => 19
         ]
         ];   



foreach ($your_array as $value){
    for($i=0;$i<=22;$i=$i+2){
    $j=$i+2;
    if($value['hour']>=$i && $value['hour']<$j){
        isset($result[$i.'-'.$j])?$result[$i.'-'.$j]+=$value['views']:$result[$i.'-'.$j]=$value['views'];
    }
    }
}

print_r($result);

3 Comments

Yes but that way I need to do if statments for all time ranges, since I don't have only the ones that I listed above in the question, I am hoping for some more elegant solution to it.
you must define exactly what you want.You want every two hours ranges for all day in 24 hour format?
Ok I updated the answer that would sum the views for every two hours "0-1","2-3","3-2",....,"22-23"
0

Try below code.

$arr = [0 => [
         "hour" => 0,
         "views" => 1
         ],
    1 => [
         "hour" => 1,
         "views" => 1
         ],
    2 => [
         "hour" => 4,
         "views" => 1
         ]];    

foreach($arr as $row)
{
    if($row['hour'] >= 0 && $row['hour'] <= 2)
    {
        $newArr['0-2'] = isset($newArr['0-2']) ? ($newArr['0-2'] + 1) : 1;
    }
    if($row['hour'] > 2 && $row['hour'] < 4)
    {
        $newArr['2-4'] = isset($newArr['2-4']) ? ($newArr['2-4'] + 1) : 1;
    }
}
print_r($newArr);

Output

Array
(
    [0-2] => 2
)

1 Comment

Same as for the other answer, these way I need to do if statments for all time ranges and since I don't have only the ones that I listed above in the question, I am hoping for some more elegant solution to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.