1

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

2
  • Your inner foreach loop should only figure out if there is an appropriate cover for the current playlist. Adding the current playlist to the output array should happen after the inner foreach loop. That way, it only happens once for each playlist, but it will happen once for each, regardless of whether a matching cover was found or not. Commented Aug 9, 2019 at 10:06
  • Can you show the example code? Commented Aug 9, 2019 at 10:09

3 Answers 3

2

Using array_walk() you can do it easily. Using $filter scope we checked playlist_id from first array and replaced cover of second array by video_key of first array.

Example:

$filter = array_column($first_array, 'video_key', 'playlist_id');
array_walk($second_array, function (&$val) use ($filter) {
    if (isset($filter[$val['playlist_id']])) $val['cover'] = $filter[$val['playlist_id']];
    else unset($val['cover']);
});

echo '<pre>', print_r($second_array);

Working demo.

Sign up to request clarification or add additional context in comments.

5 Comments

Your version is very elegant, but it does not display playlists if it does not have a cover
So if playlist_id not match from $first_array, what do you want to show?
Did you mean playlist_id?
Look, in total we have three playlists and two covers, you need to display all three playlists and covers for them, if any. if there is no cover, then the playlist [cover] = NULL
Ok, got your point. You want to remove cover if there is no matching. Right? Updated answer, please check the demo.
0

Update your loop. This should work:

foreach($playlists as $pls) {
    $NewPlayList = array(
        'id' => $pls['playlist_id'],
        'title' => $pls['playlist_title'],
        'videos' => $pls['playlist_videos'],
        'date' => strtotime($pls['date']) * 1000,
        'access' => $pls['access'],
        'cover' => null
    );

    foreach($covers as $cover) {
        if(isset($cover['playlist_id']) && ($cover['playlist_id'] == $pls['playlist_id'])) {
            $NewPlayList['cover'] = $cover['video_key'];
        }
    }
    $output['list'][] = $NewPlayList;
}

Comments

0
    return array_map(function($playlist) {
        $hasCover = array_filter($covers, function($c) use ($playlist){
            return $c['playlist_id'] === $playlist['id'];
        });
        if (count($hasCover) > 0) {
            return [
                'id' => $playlist['playlist_id'],
                'title' => $hasCover[0]['playlist_title'],
                'videos' => $hasCover[0]['playlist_videos'],
                'cover' => $hasCover[0]['playlist_videos']['cover'],
                'date' => strtotime($hasCover[0]['playlist_videos']['date']) * 1000,
                'access' => $hasCover[0]['playlist_videos']['access']
            ]
        } else {
            //playlist have no cover
            return null;
        }   
    } use ($covers));

Comments

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.