imperium2335 Posted April 4, 2009 Share Posted April 4, 2009 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 Quote Link to comment Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 Where are you getting these object names from? Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted April 4, 2009 Author Share Posted April 4, 2009 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 Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 4, 2009 Share Posted April 4, 2009 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)... Quote Link to comment Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 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. Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted April 4, 2009 Author Share Posted April 4, 2009 Hey thanks for that, didn't know that function existed. Is there a variation of explode that doesn't mean I have to lose the "-"s? because I want them in the final out put. Quote Link to comment Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 Hey thanks for that, didn't know that function existed. Is there a variation of explode that doesn't mean I have to lose the "-"s? because I want them in the final out put. Why not just add them in at the final print? Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 4, 2009 Share Posted April 4, 2009 You can use implode using '-' as the first parameter. EDIT - That is to say, just implode your array back into a string that is delimited by dashes between each entry. Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted April 4, 2009 Author Share Posted April 4, 2009 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? Quote Link to comment Share on other sites More sharing options...
Maq Posted April 4, 2009 Share Posted April 4, 2009 Implode returns a string, you need to use array_unique on an array (explode). Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 4, 2009 Share Posted April 4, 2009 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 Quote Link to comment Share on other sites More sharing options...
unrelenting Posted April 4, 2009 Share Posted April 4, 2009 To make the code colour coded, instead of putting your code within (without the space) instead... The [nobbc ] (without the space) tag allows you to give examples without bbc converting them on SMF boards. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 4, 2009 Share Posted April 4, 2009 Ah.. thanks.. I was initially trying [bbc ]...[/bbc] .. was close. Quote Link to comment Share on other sites More sharing options...
unrelenting Posted April 4, 2009 Share Posted April 4, 2009 Ah.. thanks.. I was initially trying [bbc ]...[/bbc] .. was close. Indeed. Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted April 4, 2009 Author Share Posted April 4, 2009 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" ; ?> Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 4, 2009 Share Posted April 4, 2009 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. Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted April 4, 2009 Author Share Posted April 4, 2009 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. } Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted April 4, 2009 Share Posted April 4, 2009 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. Quote Link to comment Share on other sites More sharing options...
imperium2335 Posted April 5, 2009 Author Share Posted April 5, 2009 Thanks man, that has made it a lot simpler. My little application now works thanks to all you guys for helping Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.