【发布时间】:2012-12-22 19:46:45
【问题描述】:
您好,我已经建立了一个数组,并在完成脚本的最后过程中。
我一直在构建一个网络爬虫,它可以扫描指定网站上的特定链接。爬虫将所有链接存储在一个页面中,然后将它们插入到另一个数组(news_stories)中,其中包含指定的链接。
现在新数组的结构是这样的。
Array
(
[0] => Array
(
[title] => The Qlick
[description] => Qlick
[keywords] => Search Engine, Web, Sexy, Noah Smith
[link] => http://www.theqlick.com/qlickdates.php
)
)
这对我来说很难内爆并插入到 mysql 表中。现在我下面的代码使用函数来获取它找到的每个链接的标题、描述等,然后如果它们匹配,则将它们插入到新数组中。
有没有办法让这个删除数组顶部的 Array() 并只保留指定标题、描述等的数组。 我希望输出看起来像:
[0] => Array
(
[title] => The Qlick
[description] => Qlick
[keywords] => Search Engine, Web, Sexy, Noah Smith
[link] => http://www.theqlick.com/qlickdates.php
)
有什么想法吗?
$bbc_values = array('http://www.theqlick.com/festivalfreaks.php', 'http://www.theqlick.com/qlickdates.php');
$news_stories = array();
foreach ($links as $link) {
$item = array(
"title" => Titles($link),
"description" => getMetas($link),
"keywords" => getKeywords($link),
"link" => $link
);
if (empty($item["description"])) {
$item["description"] = getWord($link);
}
foreach($bbc_values as $bbc_value) {
// note the '===' . this is important
if(strpos($item['link'], $bbc_value) === 0) {
$news_stories[] = $item;
}
}
}
$data = '"' . implode('" , "', $news_stories) . '"';
$query =
//CNN algorithm
print_r($news_stories);
【问题讨论】:
-
为什么不删除第一个 $news_stories 数组?当你用它打开时,它是一个空数组。然后,您只需使用 $news_stories[] 将其添加到数组中。只需执行 $news_stories[] 将为您创建数组
-
已经完成@MrTechie 并且似乎没有任何改变
-
什么意思?您想将数组展平为一维吗?你的输出会是什么样子?
-
@MichaelBerkowski 编辑的代码。
-
@MatthewSmith 这就是我的想法,但是您在循环中附加到
$news_stories,这意味着您期望其中不止一个。这意味着移除那个外部维度是没有意义的。 ($inner = array_pop($news_stories);很容易做到这一点,你为什么要破坏这些值?看起来你可能正在为INSERT语句建立一个VALUES ()列表,在这种情况下,你真的应该使用支持准备好的 API声明。