Noah Smith
Noah Smith

Reputation: 303

creating multidimensional array with two arrays

I am indexing web pages. The code scans the web pages for links and the web page that is given's title. The links and title are stored in two different arrays. I would like to create a multidimensional array that has the word Array, followed by the links, followed by the individual titles of the links. I have the code, I just don't know how to put it together.

     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]);

Upvotes: 0

Views: 889

Answers (2)

J. Bruni
J. Bruni

Reputation: 20492

It is not clear what you want.

Anyway, here is how I would rewrite your code in a more organized way:

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);

Upvotes: 0

Wasim
Wasim

Reputation: 5113

You should be able to do this, assuming there are the same amount of links as there are titles, then they should correspond to the same array key.

$newArray = array();

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

Upvotes: 1

Related Questions