There are two arrays, one contains lists of playlists, the second contains their covers Array with covers:
Array
(
[0] => Array
(
[id] => 110
[playlist_id] => 131
[video_key] => Jz4YS6oz
[user] => 20
[date] => 2019-08-09 12:21:40
)
[1] => Array
(
[id] => 109
[playlist_id] => 128
[video_key] => KoLwjBed
[user] => 20
[date] => 2019-08-09 11:37:50
)
)
Array with playlists:
Array
(
[0] => Array
(
[playlist_id] => 132
[playlist_title] => 222
[user] => 20
[date] => 2019-08-09 12:22:09
[cover] =>
[access] => 1
[playlist_videos] => 0
[playlist_featured] => 0
)
[1] => Array
(
[playlist_id] => 131
[playlist_title] => 111
[user] => 20
[date] => 2019-08-09 11:28:47
[cover] =>
[access] => 1
[playlist_videos] => 2
[playlist_featured] => 0
)
[2] => Array
(
[playlist_id] => 128
[playlist_title] => 333
[user] => 20
[date] => 2019-08-08 21:16:55
[cover] =>
[access] => 1
[playlist_videos] => 2
[playlist_featured] => 0
)
)
As can be seen from the code, both arrays contain the key [playlist_id]. It is necessary to somehow iterate the second array, so that the key [cover] gets the value [video_key] from the first array and the keys [playlist_id] match, if there is a playlist in the second array, but there is no cover from the first, then to the key [cover] must must contain null
I tried this varinat,
foreach($playlists as $pls){
foreach($covers as $cover){
if($cover['playlist_id'] == $pls['playlist_id']){
$output['list'][] = array(
'id' => $pls['playlist_id'],
'title' => $pls['playlist_title'],
'videos' => $pls['playlist_videos'],
'cover' => (isset($cover) && $cover['playlist_id'] == $pls['playlist_id']) ? $cover['video_key'] : NULL,
'date' => strtotime($pls['date']) * 1000,
'access' => $pls['access'],
);
}
}
}
Everything seems to be working well, but if the playlist does not have a cover, then the playlist is not displayed. If you remove the check in the second cycle, then in the final array there will be a number of elements equal to the product of the first and second arrays. For example, playlists 3, and covers 2, in the final array there will be 6 elements, some of which will be repeated ...