title: PHP handling duplicated element
body: <p>I am having an issue with the images.</p>
  
  <p>I try to add an image id when user clicks the image and the id will be saved to DB.</p>
  
  <p>When the page reload, the images that has id attribute will show the id in img tag.</p>
  
  <p>The way I identify the image that has id attribute is based on the image src. 
  However, I just found out that there are many duplicated images and my codes will add an id attribute to
  all the duplicated images. </p>
  
  <p>I wish only to add the id attribute the the image that the user clicks. </p>
  
  <p>My codes</p>
  
  <p><code>this.id</code> is the new id attribute for the image and it generated from DB.</p>
  
  <pre><code>//click image codes....
  //assign the id to the clicked image (not all duplicated images)
  this.img.attr('id',this.id);
  </code></pre>
  
  <p>when page reload...</p>
  
  <pre><code> $doc = new DOMDocument();
   $doc-&gt;loadHTML($myHtml);
  
            $imageTags = $doc-&gt;getElementsByTagName('img');
  
            //get the images that has id attribute from DB
            $imgSource=$this-&gt;imgSource;    //imgSource is an array
            $imgID=$this-&gt;imgID;            //imgID is an array
  
            //search the htmlstring and add the id attribute to the images
            foreach($imageTags as $tag) {
              $source=$tag-&gt;getAttribute('src');
  
             //if the html contains the image that has id attribute..
                if(in_array($source, $imgSource)){
  
                 $ids=array_keys($imgSource,$source);
                 foreach($ids as $id){
                    $tag-&gt;setAttribute('id',$imgID[$id]);
                    $myHtml=$doc-&gt;saveHTML();
                  }
                }
              }
            }
  </code></pre>
  
  <p>My codes above will assign the id to the images that has id stored in DB. However, it will also 
  assign the id to all the duplicated images. I need to distinquish those duplicated images and I can ONLY do it in php in my case. This problem has driven me crazy! I would really appreciate if anyone can help me about it. Thanks so much.</p>