Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8953349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:04:29+00:00 2026-06-15T14:04:29+00:00

I am trying to create a foreach which will cycle through a given list

  • 0

I am trying to create a foreach which will cycle through a given list of YouTube videos in order to populate a JW Player playlist. The structure of JW Player’s syntax allows for the inclusion of multiple videos to build a playlist, however I need to break into the foreach when it reaches the final item in order to close the playlist config.

When not being dynamically generated, the JW Player syntax looks so:

<div id='player_1465'></div>
                <script type='text/javascript'>
                  jwplayer("player_1465").setup({
                    height: 360,
                    listbar: {
                        position: 'right',
                        size: 320
                    },
                    width: 960,
                    playlist: [{
                        sources: [
                            { file: "http://www.youtube.com/watch?v=DeumyOzKqgI" }
                        ],
                        title: "Skyfall Lyric Video"
                    },{
                        sources: [
                            { file: "http://www.youtube.com/watch?v=diP-o_JxysA" }
                        ],
                        title: "Star Trek Into Darkness Announcement Trailer"
                    }]
                });
                </script>

As you can see, each ‘source’ in the playlist is closed with ‘},{‘, with the last one ending with ‘}]’. I need the foreach to know when it has loaded the final video from the given array and close the playlist section with ‘}]’.

I have got so far with the dynamically generated code:

$clips = $tmdb->getMovieTrailers($tmdb_id);

<div id='player_1465'></div>
                <script type='text/javascript'>
                  jwplayer("player_1465").setup({
                    height: 360,
                    listbar: {
                        position: 'right',
                        size: 320
                    },
                    width: 960,
                    playlist: [{
                        <?php
                            foreach($clips['youtube'] as $yt){
                                echo 'sources: [
                                            { file: "http://www.youtube.com/watch?v=' . $yt['source'] . '" }
                                        ],
                                        title: "' . $yt['name'] . '"
                                        }]'
                            }
                        ?>
                    }]
                });
                </script>

Any help given will be greatly appreciated. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:04:30+00:00Added an answer on June 15, 2026 at 2:04 pm

    You should consider using json_encode for this.

    <?php
    $clips = $tmdb->getMovieTrailers($tmdb_id);
    $playlist = array();
    foreach($clips['youtube'] as $yt) {
        $playlist[] = array(
                            'sources' => array(array('file' => 'http://www.youtube.com/watch?v=' . $yt['source'])),
                            'title' => $yt['name']
                      );
    }
    ?>
    
    <div id='player_1465'></div>
    <script type='text/javascript'>
        jwplayer("player_1465").setup({
            height: 360,
            listbar: {
                position: 'right',
                size: 320
            },
             width: 960,
             playlist: <?=json_encode($playlist)?>
        });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an array for years which i will use in
Im trying to create a mp3 player that will add songs per day. So
What I'm trying to do is create a PowerShell script which gets a list
I'm trying to create a script of which will log into drupal, and then
Im trying to create a function, that will return a mysql query, which i
I'm new to js. trying to create mini validation function which will check fields
I'm trying to create a user control which will allow me to pass in
I'm trying to create a context menu which will be able to add extra
I'm trying to create a directory and copy a file (pdf) inside a Parallel.ForEach
I am trying to create an item list, diffrent for each i and j

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.