我有一个数组,用于跟踪视频ID,标题和说明。
每次在网站上执行搜索时,阵列都会更新。 foreach循环使用搜索到的所有数据更新数组,通常在每次搜索时在数组中添加8个新行。
我想要做的是在循环的开头插入注释行,以便在我调试时更容易滚动数组等。
正在发生的事情是每次传递时都会添加注释行,因此8条新线的注释行为8条。
示例:
// 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
如何在数组中8个新行上方的循环开头添加注释行?
以下是我的代码示例。
数组:
$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'),
));
Foreach循环:
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;*/
}
}
评论专栏:
$comment_line = ' // '.$_GET['q'].'
';
答案 0 :(得分:1)
一种简单的方法是在循环开始之前打印注释。如果由于任何原因无法做到这一点,您可以使用布尔值来标记它是否是第一次迭代:
$isFirst = true;
foreach ($searchResponse['items'] as $searchResult) {
// ... prepare your output ...
if ($isFirst) {
echo ' // '.$_GET['q']."\n"; // Echo debug line
$isFirst = false;
}
// .. echo array row ..
}
但正如评论中已经提到的:切换到数据库会更容易。值得最初的努力。