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", ] ]; $allTagsResult = array(); $format = array(); foreach( $allTags as $a ) { $name = $a['name']; $videos = explode(',', $a['video']); foreach( $videos as $v ) { if( !isset( $format[$v]) ) { $format[$v] = array(); } $format[$v][] = $name; } } foreach( $format as $video => $names) { $allTagsResult[] = array('name' => implode(',', $names), 'video' => $video); } echo '<pre>'; print_r($allTagsResult); die;
Output for 7.1.26 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.26, 7.4.0 - 7.4.13, 8.0.0 - 8.0.3
<pre>Array ( [0] => Array ( [name] => TAG-ONE,TAG-TWO [video] => 64070 ) [1] => Array ( [name] => TAG-ONE,TAG-TWO [video] => 64076 ) [2] => Array ( [name] => TAG-ONE,TAG-TWO [video] => 64110 ) [3] => Array ( [name] => TAG-ONE,TAG-TWO,TAG-THREE [video] => 64111 ) )