Search code examples
phphtmlarrayssimple-html-domarray-unique

array_unique() in php simple html dom


I wrote the code blow to get all unique links from a url:

include_once ('simple_html_dom.php');

$html = file_get_html('http://www.example.com');

foreach($html->find('a') as $element){ 
  $input = array($element->href = $element->href . '<br />');
  print_r(array_unique($input));}

but I really can't understand why it shows the duplicated links too! is there any problem with the function array_unique and simple html dom? and there's another thing I guess is related to the problem: when you execute this you see all of the link that it extracted are in one key I mean this :

array(key  => all values)

Is there any one who can solve this?


Solution

  • I believe you want it more like this:

    $temp = array();
    foreach($html->find('a') as $element) { 
        $temp[] = $element->href;
    }
    
    echo '<pre>' . print_r(array_unique($temp), true) . '</pre>';