您在这里实际拥有的是您首先迭代视频,然后针对每个视频迭代评论。
所以你有一个嵌套迭代:第 1 级:视频,第 2 级:评论。
正如我之前评论的那样,您可以创建一个能够在多维数组中以这种方式迭代的数据的数据结构。让我们在以下示例中拍摄两个 Youtube 视频:
- -FRm3VPhseI:清洁代码会谈 - “全球状态和单身人士”
- RlfLCWKxHJ0:干净的代码说话 - 不要寻找东西!
因此,如果您想在这两个视频上创建一个包含所有评论的多维数组,您可以选择以下格式:
$videos = array(
'-FRm3VPhseI' => array(
'id' => '-FRm3VPhseI',
'title' => 'The Clean Code Talks - "Global State and Singletons"',
'href' => 'http://www.youtube.com/watch?v=-FRm3VPhseI&feature=youtube_gdata',
'comments' => array(
array(
'author' => 'Nelson ThePrimate',
'content' => 'There is a cost for r...',
),
array(
'author' => 'dennisdegreef',
'content' => 'That is also a global...',
),
array(
'author' => 'MorleyCode',
'content' => 'State is unavoidable,...',
),
// ...
array(
'author' => 'Jacob Jensen',
'content' => 'I don\'t quite underst...',
),
array(
'author' => 'unity20000',
'content' => 'Testing is not the on...',
),
array(
'author' => 'drummist180',
'content' => 'Turing machine > line...',
),
),
),
'RlfLCWKxHJ0' => array(
'id' => 'RlfLCWKxHJ0',
'title' => 'The Clean Code Talks - Don\'t Look For Things!',
'href' => 'http://www.youtube.com/watch?v=RlfLCWKxHJ0&feature=youtube_gdata',
'comments' =>
array(
array(
'author' => 'Nikolai Paul',
'content' => 'this guy sometimes so...',
),
array(
'author' => 'Madrid Softwaree',
'content' => 'Learn Selenium , QTP ...',
),
array(
'author' => 'Roger Keulen',
'content' => 'Di: Great as a FXCop ...',
),
// ...
array(
'author' => 'michaeldeng1981',
'content' => 'if I do outsourcing p...',
),
array(
'author' => 'Rico Lelina',
'content' => 'How about loggers? Is...',
),
array(
'author' => 'twistedbydsign99',
'content' => '11:55 it should defin...',
),
),
),
);
它首先包含由 Youtube ID 键入/索引的所有视频,然后包含在所有评论中。要输出这个,你只需要嵌套两个 foreach 子句:
foreach ($videos as $videoId => $video)
{
printf("%s: %s\n", $videoId, $video['title']);
printf(" Comments (%d):\n", count($video['comments']));
foreach ($video['comments'] as $i => $comment)
{
printf(" #%d %s: %s\n", $i + 1, $comment['author'], $comment['content']);
}
echo "\n";
}
这将创建以下输出:
-FRm3VPhseI: The Clean Code Talks - "Global State and Singletons"
Comments (6):
#1 Nelson ThePrimate: There is a cost for r...
#2 dennisdegreef: That is also a global...
#3 MorleyCode: State is unavoidable,...
#4 Jacob Jensen: I don't quite underst...
#5 unity20000: Testing is not the on...
#6 drummist180: Turing machine > line...
RlfLCWKxHJ0: The Clean Code Talks - Don't Look For Things!
Comments (6):
#1 Nikolai Paul: this guy sometimes so...
#2 Madrid Softwaree: Learn Selenium , QTP ...
#3 Roger Keulen: Di: Great as a FXCop ...
#4 michaeldeng1981: if I do outsourcing p...
#5 Rico Lelina: How about loggers? Is...
#6 twistedbydsign99: 11:55 it should defin...
请注意,在此输出中,每次评论的数量限制为 6,因为我减少了示例数组的评论数量。foreach
现在仔细比较如何使用嵌套子句读取嵌套的多维数组的结构。外部 foreach 正在阅读视频,内部 foreach 正在阅读评论。
这个顺便说一句。与构建该数组相同,它也适用于两个嵌套迭代。为了使这更简单,我首先创建了一个缓存变量和一些辅助函数:
$gdataFetchCache = [];
$gdataFetch = function ($url) use (&$gdataFetchCache)
{
if (!isset($gdataFetchCache[$url]))
{
$gdataFetchCache[$url] = simplexml_load_file($url);
}
return $gdataFetchCache[$url];
};
$gdataNamed = function ($pattern) use ($gdataFetch)
{
return function ($value) use ($pattern, $gdataFetch)
{
return $gdataFetch(sprintf($pattern, $value));
};
};
$ytVideo = $gdataNamed('http://gdata.youtube.com/feeds/api/videos/%s');
$ytComments = $gdataNamed('http://gdata.youtube.com/feeds/api/videos/%s/comments');
这些函数允许在嵌套的 foreach-es 中更轻松地获取 Youtube 数据:
$videoIds = ['-FRm3VPhseI', 'RlfLCWKxHJ0'];
$videos = [];
foreach ($videoIds as $videoId)
{
$video = $ytVideo($videoId);
$videoArray = [
'id' => (string)$videoId,
'title' => (string)$video->title,
'href' => (string)$video->link['href'],
'comments' => [],
];
$videos[$videoId] = $videoArray;
foreach ($ytComments($videoId)->entry as $comment)
{
$videos[$videoId]['comments'][] = [
'author' => (string)$comment->author->name,
'content' => (string)$comment->content,
];
}
}
如果再次仔细比较,它的结构与输出代码相同。
这是您可以读取和创建预先不知道计数的多维数组的方法。通过迭代,您可以为一个元素创建代码,但只要有元素就使用它。
这也适用于嵌套。