2012-12-22 30 views
0

嗨我已经构建了一个数组,并在完成脚本的最后过程中。更改PHP阵列

我一直在建立一个网络爬虫扫描指定网站上的特定链接。爬虫将所有链接存储在一个页面中,然后将它们插入另一个数组(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表中。现在我的代码使用函数来为它找到的每个链接抓取标题,描述等,然后将它们插入到新数组中,如果它们匹配的话。

反正有没有得到这个删除的磁盘阵列()在阵列的顶部,只是有指定标题的数组,描述等 我希望输出的样子:

[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); 
+0

为什么不移除第一个$ news_stories数组?当你打开它时,它是一个空的数组。然后,您只需使用$ news_stories []添加到数组中。只要做$ news_stories []会为你创建数组 – MrTechie

+0

已经完成@MrTechie,似乎没有任何变化 –

+0

你是什么意思?你想将阵列平铺到一个维度吗?你的输出会是什么样子? –

回答

0

PHP 的foreach()是一个迭代,意在让你的编程,实现对数据的收集同样的动作。在现有设计中,* $ news_stories *数组可以包含多个单独的“子数组”,每个这样的“子数组”都是您想要插入数据库的数据单位。

当你到有关创建查询的一部分,你可以使用这样的事情:

foreach ($news_stories as $story) 
{ 
    /* CREATE AND RUN QUERY USING DATA IN $story */ 
} 

您可以使用*的var_dump()*看一个$故事数组的内容。当你这样做时,它可能会立即有意义。 PHP foreach()并不关心你是否有一个元素或一百个元素。如果你有零个元素,它将不会在控制结构中运行代码。