Want to get Common tag (comma separated) been used for Video Ids

youtube api get video by id
get list of videos in youtube channel
youtube api v3
youtube api get video info
youtube search api example
youtube api tutorial
youtube api: search
youtube api get video url

Here is the array I am getting from the request.

$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",
            ]
        ];

I want to get the common tag name used for all the video ids, which are in comma-separated.

Expected output

$allTagsResult = [
            0 =>  [
                "name" => "TAG-ONE,TAG-TWO",
                "video" => "64070",
            ],
            1 =>  [
                "name" => "TAG-ONE,TAG-TWO",
                "video" => "64076",
            ],
            2 =>  [
                "name" => "TAG-ONE,TAG-TWO",
                "video" => "64110",
            ],
            3 =>  [
                "name" => "TAG-ONE,TAG-TWO,TAG-THREE",
                "video" => "64111",
            ],
        ];

Somehow I did use checking the value using nested loops but I wish to know if you guys can suggest any shortest method to get the expected output.

If you want to completely remove foreach() loops, then using array_map(), array_walk_recursive(), array_fill_keys() etc. can do the job. Although I think that a more straightforward answer using foreach() would probably be faster, but anyway...

$out1 = array_map(function ($data) { 
        return array_fill_keys(explode(",", $data['video']), $data['name']); },
    $allTags);

$out2 = [];
array_walk_recursive( $out1, function ( $data, $key ) use (&$out2)   {
    if ( isset($out2[$key]))    {
        $out2[$key]['name'] .= ",".$data;
    }
    else    {
        $out2[$key] = [ 'name' => $data, 'video' => $key ];
    }
} );
print_r($out2);

will give...

Array
(
    [64070] => Array
        (
            [name] => TAG-ONE,TAG-TWO
            [video] => 64070
        )

    [64076] => Array
        (
            [name] => TAG-ONE,TAG-TWO
            [video] => 64076
        )

    [64110] => Array
        (
            [name] => TAG-ONE,TAG-TWO
            [video] => 64110
        )

    [64111] => Array
        (
            [name] => TAG-ONE,TAG-TWO,TAG-THREE
            [video] => 64111
        )

)

if you want to remove the keys, then

print_r(array_values($out2));

The code could be compressed by piling all of the code onto single lines, but readability is more useful sometimes.

Videos: list | YouTube Data API, The chart parameter identifies the chart that you want to retrieve. Acceptable values are: mostPopular – Return the most popular videos for the specified content region The id parameter specifies a comma-separated list of the YouTube video ID(s) for the This parameter can only be used in a properly authorized request. just think to pass comma separated Id as a parameter or how to use comma delimited value as parameter and use that in SQL server, since there is no way to use parameter as an array so we can use string for multiple IDs or values but if it's integer then we cannot use it in IN clause in SQL Server even if it is string (varchar).

Below is one way of doing it.

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

You can check Demo

Search: list | YouTube Data API, Common use cases; Request The part parameter specifies a comma- separated list of one or more search can be used in conjunction with optional search parameters like the q parameter. The parameter value must be set to a YouTube video ID and, if you are using this Ask a question under the youtube- api tag. Rating is available when the video has been rented. how to get comma separated values in sql How To Tag YouTube Videos To Get More Views! - Duration: 4:04.

Another method if you don't like looping:

$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'];
});

Demo: https://3v4l.org/vlIks

Memb_Has_All_Tags, except_tag_id:, A comma separated list of tag names or tag ID's that the current user must not have. not: You can use the attribute not=”not”, to reverse the results � I was preparing a statistical report and was stuck in one place where I needed to convert certain rows to comma separated values and put into a single row. Lets say I have multiple currencies in a table and I would like to display in a single comma separated rows.

memb_action_link, tagids: Comma separated list of Tag ID's to add or remove from the user. ID's prefixed with “-” are This shortcode can be used anywhere you can use a link. As can be seen in the above example, each row is a new line, and each column is separated with a comma. Many online services, such as an online bank, allow its users to export tabular data from the website into a CSV file. These files can then be opened and viewed offline using a spreadsheet program, such as Microsoft Excel. Why are CSV files used?

Ghost Handlebars Theme Helpers: has, Like all block helpers, {{#has}} supports adding an {{else}} block or using ^ instead of The {{#has}} helper can be combined with internal tags, to display different information Post has tag or author; Context has slug or id; Context has any or all properties Both the tag and author attributes take a comma separated list. Autoplay When autoplay is enabled, a suggested video will automatically play next. Up next Convert a Column or Rows of Data into Comma Separated Values - Duration: 3:44.

Macros - Google Ad Manager Help, Video ID Video metadata. Video title. Custom ad ID (FreeWheel) These macros are commonly used to render creatives, ensuring they look right and work correctly. This can be helpful if you want to serve different creatives based on You can use this macro to set up a passback tag between two Ad Manager networks� A comma-separated list of tags used to tag the product. Most spreadsheet applications automatically add quotes around the tags for you. If you are using a plain text editor, then you need to manually add the quotes. For example, "tag1, tag2, tag3". Published: States whether a product is published on your online store.

Comments
  • 64070 isn't present in all videos.
  • Yes, It is not present. Actually This defines the tag I wish to use for my each video Id. So for some videos, I am using some unique tag. As you can see the last one that TAG-THREE is only for 64111 video id.
  • So basically for every video id, you want all names it is present in?
  • @vivek_23 Yep. You can see the expected output.
  • Awesome... Any other short method??
  • @PunitGajjar Nothing that comes across my mind currently.