I am a newbie in PHP in general and I have a task in which I need to make it so that the "filtered" array of links is displayed (that is, without repetitions, I use array_unique ). But when I deduce one array, all the same there are repeating links. I do not understand what I'm doing wrong, please tell me!

 <?php include_once('simple_html_dom.php'); $site='http://www.bonprix.ua'; $html = file_get_html('http://www.bonprix.ua'); foreach($html->find('a') as $element){ $str = $element->href; $str4 = substr($str, 0, 4); $arr_1 = array(); $arr_2 = array(); if ($str4 == 'http') {array_push($arr_1, $str. '<br>');} else {array_push($arr_2, $site.$element->href. '<br>');} $result = array_unique($arr_1); foreach($result as $value){echo $value;} } ?> 
  • Give an example of an array. And specify in it the duplicate links that remain after your manipulations with array_unique - rjhdby

1 answer 1

Apparently you have not quite the correct creation of the array and the output of the result. Addition and output go on each link, although as I understand it should be displayed after processing all links. Try it like this

 $site='http://www.bonprix.ua'; $html = file_get_html('http://www.bonprix.ua'); $arr_1 = array(); $arr_2 = array(); foreach($html->find('a') as $element){ $str = $element->href; $str4 = substr($str, 0, 4); if ($str4 == 'http') {array_push($arr_1, $str. '<br>');} else {array_push($arr_2, $site.$element->href. '<br>');} } $result = array_unique($arr_1); foreach($result as $value){echo $value;} ?> 
  • Thank you very much, it works, I realized my mistake! :) - Tom_4