PHP: Get array value from other array in loop by condition

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

Upvotes: 1

Views: 1040

Answers (3)

MH2K9
MH2K9

Reputation: 12039

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.

Upvotes: 2

Rehmat
Rehmat

Reputation: 5071

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;
}

Upvotes: 0

JessGabriel
JessGabriel

Reputation: 1082

    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));

Upvotes: 0

Related Questions