Jump to content

Recommended Posts

Hi, I want my array to be checked against its self. i.e. I want it to check against other objects in the area, and if they are the same, do something. Do I need a loop inside a loop?

 

Here is what i have so far:

<?PHP 
$newnameword = explode("-",$newname) ;
$newwordamnt = count($newnameword) ;

for($i=0; $i < $newwordamnt; $i++)

{
   echo "$newnameword[$i]" ;
}?>

 

Thanks in advance :)

Link to comment
https://forums.phpfreaks.com/topic/152493-solved-array-object-comparison/
Share on other sites

Hi Thanks for your reply, its just a load of strings from:

 

$newname = $firstword . $deltaword . "." . $ext ; //Build the filename.

 

I've tried this:

 

$newnameword = explode("-",$newname) ; //Break the new file name words up.

$newwordamnt = count($newnameword) ; //How many words there are in the new name.

$x = 0 ;

$z = 0 ;

while($x < $newwordamnt)

{

 

$newnameword[$z++] ;

if($newnameword[$z] == $newnameword[$x])

{

echo "duplicate found" ;

$newnameword[$x++] ;

}

 

}

 

But the page loads forever :(

Would something like this serve your purpose?

$newname = '1-2-3-4-5-6-7';
$newnameword = explode("-",$newname);
$arr = array_unique($newnameword);

if(count($arr) == count($newnameword)){
// all elements contain unique values.. do something...
} else {
// there are some elements that share identical values // do something else...
}

 

EDIT - To keep things simple, I only used numbers... feel free to play around with them (or insert names instead)...

What exactly are you going to do if there are duplicates, remove them?

 

If you want to remove then use http://us3.php.net/array_unique

 

If you want to check if there's something in an array use: http://us3.php.net/in_array

 

Hope that helps.

Hi, thanks for your help so far. Now im getting errors:

 

Warning: implode() [function.implode]: Invalid arguments passed in myfile.php on line 43

 

Warning: array_unique() [function.array-unique]: The argument should be an array in myfile.php on line 46

 

my code is now:

	$newnameword = implode("-",$newname) ; //Put the words into an array
	//$newwordamnt = count($newnameword) ; //How many words there are in the new name.

	$final = array_unique($newnameword) ;

	if(count($final) == count($newnameword))
	{
	echo "unique words" ;
	}else{
	echo "not unique words" ;
	}

 

Also can someone tell me how to make the code i put in look like the php colours? :P

implode is the inverse of explode.. it puts an array together to form a string... so that implode part should be put after the initial block of code:

 

$newnameword = explode("-",$newname);
$arr = array_unique($newnameword);

if(count($arr) == count($newnameword)){
// all elements contain unique values.. do something...
} else {
// there are some elements that share identical values // do something else...
}

$newnameword = implode("-",$newname) ; //Put the array of words into a string

 

Is that along the lines of what you are looking for? Also note that you don't have explode the actual string you started with... you can create a new string that is equal to the name string, and explode that instead (thus the original $newname is untouched).

 

To make the code colour coded, instead of putting your code within

 (without the space) instead...

 

EDIT - Yeah, what Maq just said ;)

Hi, thanks for all your help, its is coming along nicely.

 

What I'd like to happen is if there are duplicates, re-roll to get a new name that has no duplicates. Once there are no duplicate words in the name, renaming of the actual file can proceed.

 

Here is the full code:

 

<?PHP

$firstword = "birthday-" ; //The category word, first word of the image. Helps you organise/identify your images in the directory.

//////////////////
//Random words for the types of images.

$wordbox = array("cake-","celebration-","party-","unique-","bespoke-","fun-","designer-","beautiful-","modern-","designs-","cakes-","colourful-") ; //bday
//$wordbox = array("cake-","celebration-","party-") ; //wedding
//$wordbox = array("cake-","celebration-","party-") ; //choc

////////////////
$arrayamt = count($wordbox) -1; //Count how many words we have in the array and take away one so our random number only goes up to the arrays maximum (see below).

echo "$wordbox[$w]" ;
$dir = "tests" ; //Source directory.
$outdir = "tests" ; //Output directory.

$openzone = opendir($dir) ;

while($file = readdir($openzone))//While files exist in the dir.
{

$limit = rand(1,3) ; //File name lengths between 2 and 4 words

if($file != '.' && $file != '..' && !is_dir($dir.'/'.$file))//Only if a real file.
{
	for($i=0; $i<$limit; $i++) //How many words there will be in the name.
	{
		$w = rand(0,$arrayamt) ; //Pick a random word from the array.
		$deltaword .= $wordbox[$w] ; //Add the words together.
	}

	$deltaword = substr($deltaword, 0, -1) ; //Remove the last "-".

	$filecount++ ;
	$ext = end(explode(".", $dir.'/'.$file)) ; //Get the file's extension.

	$newname = $firstword . $deltaword ; //Build the filename.

////////(Below)Now check for duplicate words in the new filename.
	echo "$newname<br />" ;
	$newnameword = explode("-",$newname) ; //Put the words into an array

	$final = array_unique($newnameword) ;

	if(count($final) == count($newnameword))
	{
	echo "unique words" ;
	}else{
	echo "not unique words" ;
	}

	$newnameword = implode("-",$newname) ;

	echo "$newnameword" ;

	/*rename($dir.'/'.$file, $outdir.'/'.$newname) ; //Rename the file.
	if($ext == "jpg" || $ext == "bmp" || $ext == "gif")
	{
		echo "The image ($ext) $file has been renamed.<br />" ;
	}
	else
	{
		echo "The file $file has been renamed.<br />" ;
	}
	echo "The new file name is: $firstword$deltaword.$ext<br /><br />" ;*/
	$deltaword = NULL ; //Reset file name for next file.
}
}

echo "$filecount files have been renamed" ;

?>

echo "$wordbox[$w]" ;

$deltaword .= $wordbox[$w] ;

 

From your snippet, I don't see where $w comes from... but maybe that part is from higher up in your code not posted, so perhaps that isn't important.. I just ask because you included that in the snippet..

 

As far as the array_unique stuff is concerned, since all you are doing is outputting "unique words" or "not unique words", the array that is $newnameword serves no real purpose after the if statement. So I'm not sure why you would impode it and echo it out onscreen (now that I'm seeing this code you provided). So since you don't want duplicates in $newname, if I understand correctly, would this reworked part work for what you need?

 

$newnameword = explode("-",$newname) ; //Put the words into an array
$final = array_unique($newnameword) ;
if(count($final) != count($newnameword)){ // which would mean that $final is smaller (thus, all unique values)
$newname = implode("-", $final); // assign $newname to have only the unique values of $final
}

 

then do whatever with $newname.

Hi, Thanks.

 

$w comes from this bit:

 

if($file != '.' && $file != '..' && !is_dir($dir.'/'.$file))//Only if a real file.
{
	for($i=0; $i<$limit; $i++) //How many words there will be in the name.
	{
		$w = rand(0,$arrayamt) ; //Pick a random word from the array.
		$deltaword .= $wordbox[$w] ; //Add the words together.
	}

Glad to see you resolved it.

You know, I'm tired.. I looked over my previous code.. so instead of that whole if statement, this solution would be simpler:

 

$newname = implode('-',array_unique(explode('-',$newname)));

 

Man, I over-complicated things. My bad. This will teach me to stop when I'm getting tired.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.