Help me please.

There is an array of 1:

Array ( [0] => Array ( [cnt] => 4 //общая сумма просмотров всех видео на странице подсчитанных из массива 2 [id_page] => 363 //страница на которой было просмотрено всего "cnt"-4 видео ) [1] => Array ( [cnt] => 3 [id_page] => 102 ) ) 

There is an array of 2:

 Array ( [0] => Array ( [id] => 1 // порядковый номер [data_click] => 1535215098 // время когда было просмотрено видео [id_video] => 563 //ID просмотренного видео [id_page] => 363 //Страница на которой было просмотрено видео ) [1] => Array ( [id] => 2 [data_click] => 1535215549 [id_video] => 564 [id_page] => 363 ) [2] => Array ( [id] => 3 [data_click] => 1535215935 [id_video] => 563 [id_page] => 363 ) [3] => Array ( [id] => 4 [data_click] => 1535216040 [id_video] => 563 [id_page] => 363 ) [4] => Array ( [id] => 5 [data_click] => 1535743017 [id_video] => 564 [id_page] => 103 ) [5] => Array ( [id] => 6 [data_click] => 1535743024 [id_video] => 564 [id_page] => 103 ) [6] => Array ( [id] => 7 [data_click] => 1535743113 [id_video] => 563 [id_page] => 103 ) 

How do I make array 3, so that there would be grouped data from these two arrays? The output should be like this: Array 3:

 Array ( [0] => Array ( [cnt] => 4 // Сумма всех просмотренных видео на странице [id_page] => 363 // Страница на которой были просмотрены видео [more_video] => Array //Массив где просмотры разбиваем по каждому видео ( [0] => Array ( [id_video] => 563 //ID видео [show_video] => 2 // Кол-во просмотров этого видео на странице с ID 363 ) [1] => Array ( [id_video] => 564 [show_video] => 2 ) ) ) [1] => Array ( [cnt] => 3 [id_page] => 103 [more_video] => Array ( [0] => Array ( [id_video] => 563 [show_video] => 2 ) [1] => Array ( [id_video] => 564 [show_video] => 1 ) ) ) ) 
  • And where is the value of show_video in the show_video data? I do not watch it, and explain more specifically how you want to do it. - Let's say Pie
  • Thanks for the answer: 1 array "cnt" is the sum of views of the video "id_video" on each page "id_page" from the 2nd array. And "show_video" is the sum of views of a specific video "id_video" on this page "id_page" - Kostas Grek
  • Added comments in the description yet, just in case - Kostas Grek

1 answer 1

Initially, we have 2 arrays:

 $arr1 = [ ['cnt' => 4, 'id_page' => 363], ['cnt' => 3, 'id_page' => 103] ]; $arr2 = [ ['id' => 1, 'data_click' => 1535215098, 'id_video' => 563, 'id_page' => 363], ['id' => 2, 'data_click' => 1535215549, 'id_video' => 564, 'id_page' => 363], ['id' => 3, 'data_click' => 1535215935, 'id_video' => 563, 'id_page' => 363], ['id' => 4, 'data_click' => 1535216040, 'id_video' => 563, 'id_page' => 363], ['id' => 5, 'data_click' => 1535743017, 'id_video' => 564, 'id_page' => 103], ['id' => 6, 'data_click' => 1535743024, 'id_video' => 564, 'id_page' => 103], ['id' => 7, 'data_click' => 1535743113, 'id_video' => 563, 'id_page' => 103], ]; 

Next, we perform the necessary transformations:

 foreach ($arr1 as $page) { foreach ($arr2 as $click) { if ($click['id_page'] === $page['id_page']) { if (!isset($tmp[$click['id_video']])) { $tmp[$click['id_video']] = 0; } $tmp[$click['id_video']]++; } } $more_video = []; foreach ($tmp as $id => $count) { $more_video[] = [ 'id_video' => $id, 'show_video' => $count, ]; } $page['more_video'] = $more_video; $result[] = $page; } 
  • For the first iteration, it works perfectly, and from the second it starts to copy and supplement the data from previous iterations - Kostas Grek
  • @KostasGrek, fixed - Let's say Pie