duanpu5048 2015-07-09 06:35
浏览 55
已采纳

如何在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'].'
';
  • 写回答

1条回答 默认 最新

  • douyan4243 2015-07-09 06:53
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?