I've looked all over the web for a reason why I can't remove duplicates from my array. I am aware of array_unique but it's not working.
I have an array called $links which is populated from the links found in html source code
If i then echo out
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $html, $links, PREG_SET_ORDER)) {
foreach($links as $link) {
echo($link[2]."<br />"); // $link[2] = address, $link[3] = text
}
}
it outputs each link to a new line perfectly, however there are duplicates
$link[2] is the only dimension of the array i am interested in, and i cannot remove its duplicates, can anyone help?
This is what is echoed out, for example
/
/register
/forgotpass
/
/news
/news
/news/submit_article
#
/recruiters/companies
/recruiters/headhunters
/jobs
/jobs
/jobs/submit_job
/about
/jobs
/jobs/submit_job
/resume
/register
/fullmap
I would hope to see
/
/register
/forgotpass
/news
/news/submit_article
#
/recruiters/companies
/recruiters/headhunters
/jobs
/about
/jobs/submit_job
/resume
/fullmap
FIXED BY JON - ANSWER:
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $html, $links, PREG_SET_ORDER)) {
$unique_links = array_unique(array_map(function($item) { return $item[2]; }, $links));
foreach($unique_links as $link) {
echo($link."<br />");
}
}
$html
that demonstrates this problem? – deceze Dec 8 '11 at 23:41