Jump to content
IvanGS

elminiar valores repetidos num array

Recommended Posts

IvanGS

Boas pessoal preciso de ajuda numa coisa.

Preciso de eliminar valores repetidos num array, mas quero guadar um valor.

Isto lista-me os directorios recursivamente, e guarda-os num XML. o problema e que repete várias vezes a mesma coisa. o código é este:

<?php
$BASEDIR="clientes/018/PDF";
function recursedir($BASEDIR) 
{

	$server = "127.0.0.1";
	$dbuser = "root";
	$dbpassword = "";
	$database = "ezteammax";
	/* Prepare Database Connection */
	$connection = mysql_connect(''.$server.'',''.$dbuser.'',''.$dbpassword.'');
	if (!$connection) {
		die(''.$error1.' ' . mysql_error());
	}
	/* Define Database Table */
	$db_selected = mysql_select_db(''.$database.'', $connection);
	if (!$db_selected) {
		die (''.$error2.' ' . mysql_error());
	}

	$hndl=opendir($BASEDIR);
	while($file=readdir($hndl)) 
		{
			if ($file=='.' || $file=='..')
			continue;
			//lista os ficheiros e directorios
			$completepath="$BASEDIR/$file";
			//começa XML
			$dom = new DOMDocument('1.0');
			$root = $dom->createElement('root');

			if (is_dir($completepath)) 
				{
					# its a dir, recurse.
					recursedir($BASEDIR.'/'.$file, $dom, $root);
					//print "<br>DIR: $BASEDIR/$file<br><br>\n";
				} 
			else 
				{
					//print "FILE: $BASEDIR/$file<br>\n";
					$variavel = $file;
				}
				$query = mysql_query("INSERT INTO files (file, path) VALUES ("."'".$file."'".", "."'".$BASEDIR."'".")");
		}
}
recursedir($BASEDIR);
$getdata = mysql_query("SELECT * FROM files");

	$filenameopen = "pdffiles.xml";
	$fh = fopen($filenameopen, "w");
	$stringData = '<?xml version="1.0" encoding="utf-8"?>';
	fwrite($fh, $stringData);
	$stringData = "<root>";
	fwrite($fh, $stringData);

	while($row = mysql_fetch_array($getdata))
		{
			$tipefile = $row['file'];
			$tipefilexploded = explode(".", $tipefile);

			$tipepath = $row['path'];
			$tipepathexploded = explode("/", $tipepath);

			unset($tipepathexploded[0]);
			unset($tipepathexploded[1]);
			unset($tipepathexploded[2]);
			$lengtharray = count($tipepathexploded);

				if (@$tipefilexploded[1] == "pdf" or @$tipefilexploded[1] == "PDF")
						{	
							$i = 0;
							foreach ($tipepathexploded as $tiobin)
								{
									print_r(array_unique($tipepathexploded));
									//echo "DIR: ".$tiobin."<br/>";
									$stringData = "<node label="."'".$tiobin."'".">";
									fwrite($fh, $stringData);
								$i++;
								}

								if ($i == $lengtharray)
									{
										//echo "FILE: ".$row['file']."<br/>";
										$stringData = "<node label="."'".$row['file']."'"."/>";
										fwrite($fh, $stringData);
										for ($n = 2; $n<$i; $n++)
											{
												$stringData = "</node>";
												fwrite($fh, $stringData);
											}
										//echo "Chegou ao fim do array <br />";
									}
							echo "<br/>";
						}
					else
						{

						}
					$stringData = "</node>";
					fwrite($fh, $stringData);
		}


	$deletedata = mysql_query("TRUNCATE TABLE 'files'");
	$stringData = "</root>";
	fwrite($fh, $stringData);
	fclose($fh);
?>

é lenta a script, pq já m estava a passar e fiz aí uma aldrabice ...

Share this post


Link to post
Share on other sites
mAiN_iNfEcTiOn

Se isto não resolver:

# Onde tens:
$getdata = mysql_query("SELECT * FROM files");

# Muda para:
$getdata = mysql_query("SELECT DISTINCT * FROM files");

Experimenta:


# Onde tens:
while($row = mysql_fetch_array($getdata))

# Altera para:
$array_values = array();
while($row = mysql_fetch_array($getdata))
    $array_values[$row['file']] = $row;
        
foreach($array_values as $row)

Share this post


Link to post
Share on other sites
mAiN_iNfEcTiOn

Se isto não resolver:

# Onde tens:
$getdata = mysql_query("SELECT * FROM files");

# Muda para:
$getdata = mysql_query("SELECT DISTINCT * FROM files");

Esta solução, é óbvia... Para linhas repetidas, ele so mostra 1 vez... é veres a sintaxe DISTINCT no SELECT do MySQL

Experimenta:


# Onde tens:
while($row = mysql_fetch_array($getdata))

# Altera para:
$array_values = array();
while($row = mysql_fetch_array($getdata))
    $array_values[$row['file']] = $row;
        
foreach($array_values as $row)

Neste caso, ele o que faz é cria um array com o indice com o nome do ficheiro... Isto assumindo que cada ficheiro é único (segundo as tuas repetições).

E nesse array guarda a linha... Assim asseguras-te que não existe registos 'duplicados'.

Mas caso esteja errado, corrige-se não há problema

Share this post


Link to post
Share on other sites
IvanGS

tentei o teu código mas não funcionou...

não sei se o meti mal ou assim mas nada...

alterei e neste momento está assim:

<?php
$BASEDIR="clientes/018/PDF";
function recursedir($BASEDIR) 
{

	$server = "127.0.0.1";
	$dbuser = "root";
	$dbpassword = "";
	$database = "ezteammax";
	/* Prepare Database Connection */
	$connection = mysql_connect(''.$server.'',''.$dbuser.'',''.$dbpassword.'');
	if (!$connection) {
		die(''.$error1.' ' . mysql_error());
	}
	/* Define Database Table */
	$db_selected = mysql_select_db(''.$database.'', $connection);
	if (!$db_selected) {
		die (''.$error2.' ' . mysql_error());
	}

	$hndl=opendir($BASEDIR);
	while($file=readdir($hndl)) 
		{
			if ($file=='.' || $file=='..')
			continue;
			//lista os ficheiros e directorios
			$completepath="$BASEDIR/$file";
			//começa XML
			$dom = new DOMDocument('1.0');
			$root = $dom->createElement('root');

			if (is_dir($completepath)) 
				{
					# its a dir, recurse.
					recursedir($BASEDIR.'/'.$file, $dom, $root);
				} 
			else 
				{
					//print "FILE: $BASEDIR/$file<br>\n";
					$variavel = $BASEDIR."/".$file;
					//echo $variavel."<br />";
				}
				$query = mysql_query("INSERT INTO files (file) VALUES ("."'".$BASEDIR."/".$file."'".")");
		}
}
recursedir($BASEDIR);
$getdata = mysql_query("SELECT DISTINCT * FROM files");

	$filenameopen = "pdffiles.xml";
	$fh = fopen($filenameopen, "w");
	$stringData = '<?xml version="1.0" encoding="utf-8"?>';
	fwrite($fh, $stringData);
	$stringData = "<root>";
	fwrite($fh, $stringData);

		$array_values = array();
		while($row = mysql_fetch_array($getdata))
		{
			$tipefile = $row['file'];

			$topathxploded = explode("/", $tipefile);

			$pathlength = count($topathxploded);
			unset($topathxploded[0]);
			unset($topathxploded[1]);
			unset($topathxploded[2]);

			$tofilexploded = explode(".", $topathxploded[$pathlength-1]);
			if (@$tofilexploded[1] == "pdf" or @$tofilexploded[1] == "PDF")//Se o ficehrio for .pdf
			{
				for ($i = 3; $i < $pathlength; $i++)
					{
						$stringData = "<node label="."'".$topathxploded[$i]."'".">";
						fwrite($fh, $stringData);
						echo "DIR: ".$topathxploded[$i]."<br />";
						echo "DIR: ".$topathxploded[3]."<br />";
						if ($topathxploded[$i] == $topathxploded[3])
							{
								echo "<br />";
								echo "ATENÇÃO! <br />";
								echo "<br />";
							}
					}
						$stringData = "<node label="."'".$tofilexploded[0].".".$tofilexploded[1]."'"."/>";
						fwrite($fh, $stringData);
						echo "FILE: ".$tofilexploded[0].".".$tofilexploded[1]."<br />";
						echo "<br />";

				for ($n = 3; $n < $pathlength; $n++)
				{
					$stringData = "</node>";
					fwrite($fh, $stringData);
				}	
			}
		}
	$deletedata = mysql_query("TRUNCATE TABLE 'files'");
	$stringData = "</root>";
	fwrite($fh, $stringData);
	fclose($fh);
?>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.