如何在foreach循环的开头插入注释行,而不是在循环的每次传递中?

I have an array I use to keep track of video ID's, titles, and descriptions.

The array gets updated every time a search is performed on the website. A foreach loop updates the array with all the data that was searched for, usually 8 new lines added in the array on each search.

What I want to do is insert a comment line at the beginning of the loop to make it easier to scroll through the array when I'm debugging, etc.

What is happening is the comment line is being added on every pass through, so 8 comment lines for the 8 new lines.

Example:

// comment line
'1QmRjtsw2UQ' => array('pubdate' => '26 Jun 15', 'alt' => '8 Year Old Beautifully Covers Thinking Out Loud', 'description' => '8-yo \'Thinking Out Loud\''),
// comment line
'eKqLaYrcf3A' => array('pubdate' => '25 Jun 15', 'alt' => 'Plane Lands On Truck', 'description' => 'Plane Lands On Truck'),
// comment line
'B5_8D8HCnS4' => array('pubdate' => '24 Jun 15', 'alt' => 'Unbelievable Boeing 787 \'Vertical\' Take-off - Paris Air Show 2015', 'description' => '787 "Vertical" Take-Off'),
// comment line
'VQcNCc3Icx4' => array('pubdate' => '24 Jun 15', 'alt' => 'Dog Pops 54 Balloons In 3.3 Seconds', 'description' => 'Dog Pops 54 Balloons In 3 Sec'),
// comment line
'V1W-OC7MTCY' => array('pubdate' => '24 Jun 15', 'alt' => 'Lucky Lou', 'description' => 'Lucky Lou'),
// comment line
'zhbBuWSPbBk' => array('pubdate' => '23 Jun 15', 'alt' => 'Mentalist Oz Pearlman Reads Mel B\'s Mind - America\'s Got Talent 2015', 'description' => 'Oz Reads Mel B\'s Mind'),
// comment line
'SwhtbtSmcDs' => array('pubdate' => '23 Jun 15', 'alt' => 'Heidi Klum Hits Golden Buzzer for 11-Year-Old Opera Singer Arielle Baril', 'description' => '11-Year-Old Opera Singer'),
// comment line
'J_8mdH20qTQ' => array('pubdate' => '22 Jun 15', 'alt' => 'Stowaway Cat Surprises Pilot During Take-Off', 'description' => 'Stowaway Cat Surprise'),
// comment line

How can I just add the comment line at the beginning of the loop above the 8 new lines in the array?

Here are the examples of my code below.

The Array:

$videoids = new ArrayIterator(array(
// comment line
'1QmRjtsw2UQ' => array('pubdate' => '26 Jun 15', 'alt' => '8 Year Old Beautifully Covers Thinking Out Loud', 'description' => '8-yo \'Thinking Out Loud\''),
'eKqLaYrcf3A' => array('pubdate' => '25 Jun 15', 'alt' => 'Plane Lands On Truck', 'description' => 'Plane Lands On Truck'),
'B5_8D8HCnS4' => array('pubdate' => '24 Jun 15', 'alt' => 'Unbelievable Boeing 787 \'Vertical\' Take-off - Paris Air Show 2015', 'description' => '787 "Vertical" Take-Off'),
'VQcNCc3Icx4' => array('pubdate' => '24 Jun 15', 'alt' => 'Dog Pops 54 Balloons In 3.3 Seconds', 'description' => 'Dog Pops 54 Balloons In 3 Sec'),
'V1W-OC7MTCY' => array('pubdate' => '24 Jun 15', 'alt' => 'Lucky Lou', 'description' => 'Lucky Lou'),
'zhbBuWSPbBk' => array('pubdate' => '23 Jun 15', 'alt' => 'Mentalist Oz Pearlman Reads Mel B\'s Mind - America\'s Got Talent 2015', 'description' => 'Oz Reads Mel B\'s Mind'),
'SwhtbtSmcDs' => array('pubdate' => '23 Jun 15', 'alt' => 'Heidi Klum Hits Golden Buzzer for 11-Year-Old Opera Singer Arielle Baril', 'description' => '11-Year-Old Opera Singer'),
'J_8mdH20qTQ' => array('pubdate' => '22 Jun 15', 'alt' => 'Stowaway Cat Surprises Pilot During Take-Off', 'description' => 'Stowaway Cat Surprise'),
));

The Foreach Loop:

    foreach ($searchResponse['items'] as $searchResult) {
      switch ($searchResult['id']['kind']) {
        case 'youtube#video':
          $videos .= sprintf('  <a data-rel="video" href="/%s"><img alt="%s" title="%s" src="https://i.ytimg.com/vi/%s/mqdefault.jpg" width="100" height="56"> <span style="display:none;">%s</span></a>
            ', $searchResult['id']['videoId'], clean($searchResult['snippet']['title']), clean($searchResult['snippet']['title']), $searchResult['id']['videoId'], clean($searchResult['snippet']['title']));

          $new_array_lines = "  '".$searchResult['id']['videoId']."' => array('pubdate' => '".date("d M y", strtotime($searchResult['snippet']['publishedAt']))."', 'alt' => '".addslashes(clean($searchResult['snippet']['title']))."', 'description' => '".addslashes(clean_both($searchResult['snippet']['description']))."'),";

          //file_put_contents($videoids_file, $new_array_lines.PHP_EOL , FILE_APPEND | LOCK_EX);

            if(array_key_exists($searchResult['id']['videoId'], $videoids)) {
                // do nothing
            } else {
                $id_content = file($videoids_file); // Parse file into an array by newline
                $id_data = array_pop($id_content);

                if (trim($id_data) == '));?>') {
                    $id_content[] = $new_array_lines;
                    $id_content[] = '
'.$id_data;
                    file_put_contents($videoids_file, implode($id_content));
                }
            }

          break;
        /*case 'youtube#channel':
          $channels .= sprintf('<li>%s (%s)</li>', $searchResult['snippet']['title'],
            $searchResult['id']['channelId']);
          break;
        case 'youtube#playlist':
          $playlists .= sprintf('<li>%s (%s)</li>', $searchResult['snippet']['title'],
            $searchResult['id']['playlistId']);
          break;*/
      }
    }

The comment line:

    $comment_line = '   // '.$_GET['q'].'
';

An easy way would be to print the comment just before the loop starts. If this is not possible for any reason, you can just use a boolean to mark whether it is the first iteration or not:

$isFirst = true;
foreach ($searchResponse['items'] as $searchResult) {
   // ... prepare your output ...
   if ($isFirst) {
      echo '   // '.$_GET['q']."
"; // Echo debug line
      $isFirst = false;
   }
   // .. echo array row ..
}

But as already mentioned in the comments: Switching to a database would make it much easier. It's worth the initial effort.