Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用两个数组创建多维数组_Php_Arrays - Fatal编程技术网

Php 使用两个数组创建多维数组

Php 使用两个数组创建多维数组,php,arrays,Php,Arrays,我正在为网页编制索引。代码扫描网页以查找链接和给定标题的网页。链接和标题存储在两个不同的数组中。我想创建一个多维数组,其中包含单词数组,后面是链接,后面是链接的单独标题。我有密码,我只是不知道如何组合 require_once('simplehtmldom_1_5/simple_html_dom.php'); require_once('url_to_absolute/url_to_absolute.php'); //links $links = Array(); $URL = 'h

我正在为网页编制索引。代码扫描网页以查找链接和给定标题的网页。链接和标题存储在两个不同的数组中。我想创建一个多维数组,其中包含单词数组,后面是链接,后面是链接的单独标题。我有密码,我只是不知道如何组合

     require_once('simplehtmldom_1_5/simple_html_dom.php');
require_once('url_to_absolute/url_to_absolute.php');
 //links
$links = Array();
$URL = 'http://www.youtube.com'; // change it for urls to grab  
// grabs the urls from URL 
$file  = file_get_html($URL);
foreach ($file->find('a') as $theelement) {
   $links[] = url_to_absolute($URL, $theelement->href);
} 
print_r($links);
   //titles
  $titles = Array();
  $str = file_get_contents($URL);  
  $titles[] = preg_match_all( "/\<title\>(.*)\<\/title\>/", $str, $title );

   print_r($title[1]);
require_once('simplehtmldom_1_5/simple_html_dom.php');
需要一次(url_to_absolute/url_to_absolute.php);
//链接
$links=Array();
$URL='1http://www.youtube.com'; // 更改它以获取URL
//从URL获取URL
$file=file\u get\u html($URL);
foreach($file->find('a')作为$theelement){
$links[]=url\u到绝对值($url,$theelement->href);
} 
打印(链接);
//头衔
$titles=Array();
$str=文件获取内容($URL);
$titles[]=preg\u match\u all(“/\(.*)\/”,$str,$title);
印刷品($title[1]);

您应该能够做到这一点,假设链接的数量与标题的数量相同,那么它们应该对应于相同的数组键

$newArray = array();

        foreach ($links as $key=>$val)
        {
            $newArray[$key]['link'] = $val;
            $newArray[$key]['title'] = $titles[$key];
        }

你想要什么还不清楚

无论如何,以下是我将如何以更有条理的方式重写您的代码:

require_once('simplehtmldom_1_5/simple_html_dom.php');
require_once('url_to_absolute/url_to_absolute.php');

$info = array();

$urls = array(
    'http://www.youtube.com',
    'http://www.google.com.br'
);

foreach ($urls as $url)
{
    $str = file_get_contents($url);
    $html = str_get_html($str);

    $title = strval($html->find('title')->plaintext);

    $links = array();
    foreach($html->find(a) as $anchor)
    {
        $links[] = url_to_absolute($url, strval($anchor->href));
    }
    $links = array_unique($links);

    $info[$url] = array(
        'title' => $title,
        'links' => $links
    );
}

print_r($info);

你能举一个例子说明你希望它输出什么吗?你正在抓取的HTML是什么样子的?使用DOM解析器检索
标记,然后单独使用正则表达式检索
标记,这种方法似乎有缺陷。并给出一个你的输出应该是什么样的例子。是的,请给出一个你想要的输出的例子。诚恳地说,你目前的描述令人费解。我想说的例子是:Array=>=>google上面的脚本中没有可显示的标题。它创建的正是我想要的,只是它没有扫描url中的标题并将其返回到标题值