Unset in PHP- simpleXML

Discussion in 'PHP' started by protocol96, Sep 2, 2009.

  1. #1
    Here's the code to check the image mentioned in the XML and verify it against the images in the "images folder". If the image is not present unset the tag. The problem is that I have to keep running it multiple times to ensure all the redundant image tags are removed. Does anyone know why and possible fixes?

    
    <?php
    $countImageFile		=	Array();
    $xmlList	=	shell_exec("ls /var/www/html/xml/*.xml ");
    $xmlList	=	explode("\n",$xmlList);
    
    $i=0;
    
    foreach($xmlList as $file)
    {	
    Mycount($file,$fp);
    }
    
    echo $final="total images: ".count($countImageFile)."\t total existing images: ".$i."\n";
    
    
    function Mycount($file,$fp)
    {	global $i;
    	global $countImageFile;
    	 
    	$doc1              	= simplexml_load_file("$file") or die("dead in loading");
    	
    	
    	foreach( $doc1->xpath("//data") as $data)
    	{	
    
    		$image	=	$data->image;
    		if($image)
    		{
    			$image	=	(string) $image; 
    			
    			if(!in_array($image,$countImageFile))
    			{
    				$countImageFile[]=$image;
    
    				if(file_exists("/var/www/html/xml/images/$image"))
    				{
    					$i++;
    					
    				}
    				else
    				{
    					unset($data->image[0]);
    				echo	$final_1="$image Not Exist\n";
    				}
    		
    			}
    		}
    		
    	}
    $data1= $doc1->asXML();
    $fp1=fopen("$file",'w') or die('error in open file');
    fwrite($fp1,$data1) or die('error in write file');
    fclose($fp1);
    }
      
    ?> 
    
    PHP:
     
    protocol96, Sep 2, 2009 IP