3v4l.org

run code in 300+ PHP versions simultaneously
<?php $allTags = [ 0 => [ "name" => "TAG-ONE", "video" => "64070,64076,64110,64111", ], 1 => [ "name" => "TAG-TWO", "video" => "64070,64076,64110,64111", ], 2 => [ "name" => "TAG-THREE", "video" => "64111", ] ]; $video_ids = array_flip(array_unique(explode(",",implode(",",array_column($allTags,'video'))))); $result = array_map(function($id){ return ['name' => '','video' => $id]; },array_flip($video_ids)); array_walk($allTags,function($tag_data) use (&$result,&$video_ids){ $ids = explode(",",$tag_data['video']); foreach($ids as $id) $result[$video_ids[$id]]['name'] = empty($result[$video_ids[$id]]['name']) ? $tag_data['name'] : $result[$video_ids[$id]]['name'] . "," . $tag_data['name']; }); print_r($result);