查找数组中图像的重复值

I am trying to work out a problem with my array, basically I get all links on a page and echo them to the screen. First I store them in an array to catch duplicates and echo that array to the screen.

What I am trying to do is remove duplicate links but I need to allow a duplicate link only if an image is found because I need to show the image. I don't know which order they will come, I can only assume they will be after one another.

Code as follows:

foreach($body->find('a') as $a){
    $img= $a->find('img',0);//set img flag
    if(!isset($links[$a->href]) && empty($img)){//check for duplicate keys and no images
       $links[$a->href] = array('parent_tag'=>$a->parent()->tag, 
                                                 'text'=>trim($a->plaintext),
                                                 'link'=>$href,'img'=>$img);
       echo '<p><a href="'.$links[$a->href]['link'].'">'.$links[$a->href]['text'].'</a>';
    }
    elseif(!empty($img)){//deal with all images here
        ///show my image links but if it is a duplicate href it will be caught on the !isset above
    }
 }

this works fine so long as the story insn't repeated with an image twice, meaning. If i have the breaking news story at the top of the page, i get the text link and image from the code BUT if i go further down the page and the same story and image is repeated i get only the image because the text link is already set BUT i have said, check for every image and if an image show it SO i am not sure how to deal with this scenario,

Is there a better way of doing it, i just cant seem to think it through correctly.

i could have different ordering

<a href="link1.com"><img src="myHeadlineImage.jpg"></a>
<a href="link1.com">My Headling</a>

<a href="link1.com">My Headling</a>
<a href="link1.com"><img src="myHeadlineImage.jpg"></a>

just in case anyone finds this useful, i had to re-write my code with slightly different logic. The above code worked but only if the story (link and supporting image) appeared on the pages ONLY ONCE. Example, at the top of the page i could have a textlink

Most Popular

My Story 1

My Story 2

further down the story is repeated with its supporting image, resulting in 3 links to the same page, my original code would show the image by itself further down the page without any supporting text, thus not very useful for the user so here are amendments

if(!empty($img){

   if(!isset($array[$a->href])) {//if not set, set it
                      $array[$a->href] = array('text'=>'',//important for text links
                                                'link'=>$href, 
                                                 'img_src'=>$img->src,
                                                 'img'=>$a);

    elseif(isset($array[$a->href])&& empty($array[$a->href]['img'])){ 
          $array[$a->href]['img']=$a;
          $array[$a->href]['img_src']=$img->src;

     }

}//end if img  

elseif(empty($img)){

    if(!isset($array[$a->href])) {//if not set, set it
        $array[$a->href] = array('text'=>$a->plaintext,
                                   'link'=>$a->href,                 
                                    img_src=>'',                
                                    'img'=>'');                

     }
     //it was probably set by an image first so we just add the text
     elseif(isset($array[$a->href])&& empty($array[$a->href]['text'])) {
                                $array[$a->href]['text']=$a->plaintext;

     }

 }//end if (not image)                   

 }