Good day! Tell me how to mix data exactly by the date of publication, and not by section? Everything is displayed, moreover, in time, but this way: first News 10 news, then Directway 10 news and so on ... and you need one tape and a row by date.

There is a widget:

public function run() { $items = array_merge( News::find() ->where(['visible' => 1, 'deleted' => 0]) ->orderBy('time_create DESC') ->limit($this->count) ->all(), Directway::find() ->where(['visible' => 1, 'deleted' => 0]) ->orderBy('time_create DESC') ->limit($this->count) ->all(), Lifestyle::find() ->where(['visible' => 1, 'deleted' => 0]) ->orderBy('time_create DESC') ->limit($this->count) ->all() ); return $this->render('newsList', [ 'items' => $items, ]); } 

There is a view

  <?php foreach ($items as $item): ?> <?= $item->name ?> <?= Yii::$app->formatter->asDatetime($item->time_create, 'dd.MM, HH:mm') ?> <?php endforeach; ?> 
  • And merdzh on what value occurs, the key of the given arrays is time, or what? Check the array_merge documentation. To achieve the result, it is necessary that the key be a date, and a sorting for output would be made according to it. - Daniel Protopopov
  • Thanks for the tip, I'll try. - Res
  • Thank you, figured out! - Res

1 answer 1

Thanks to Daniel Protopopov for the tip! So, the solution for mixing arrays with a specific key and output in order, regardless of the location of the tables. In my case, the widget:

 public function run() { $items = ArrayHelper::merge( News::find() ->where(['visible' => 1, 'deleted' => 0]) ->orderBy('time_create DESC') ->limit($this->count) ->all(), Directway::find() ->where(['visible' => 1, 'deleted' => 0]) ->orderBy('time_create DESC') ->limit($this->count) ->all(), Lifestyle::find() ->where(['visible' => 1, 'deleted' => 0]) ->orderBy('time_create DESC') ->limit($this->count) ->all() ); ArrayHelper::multisort($items, ['time_create'], [SORT_DESC]); return $this->render('newsList', [ 'items' => $items, ]); } 

AND

In VIEW

 <?php foreach ($items as $item): ?> <?= $item->name ?> <?= Yii::$app->formatter->asDatetime($item->time_create, 'dd.MM, HH:mm') ?><?php endforeach; ?>