dijix316 Posted October 8, 2008 Share Posted October 8, 2008 So I've searched php.net and google for a good hour and can't seem to find a solution to my problem. Any help would be greatly appreciated. I have an array that has duplicate values which I need to be unique, but I don't wan't to use array_unique, because that just deletes the duplicates. I want to add a number to each duplicate that is found in the array. Hope that makes sense? For example: $filename['somefile.txt'] $filename['somefile.txt'] $filename['somefile.txt'] should be... $filename['somefile.txt'] $filename['somefile1.txt'] $filename['somefile2.txt'] Again, any help would be great. If you need any more info, just let me know. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 Why are the keys the names of files? The file names should just be IN the array as values: $files = array('somefile.txt', 'anotherfile.txt', 'testing.php', 'php.ini'); Do print_r() on that to see what I mean. Quote Link to comment Share on other sites More sharing options...
dijix316 Posted October 8, 2008 Author Share Posted October 8, 2008 Sorry, i'm still new at php...I just got lazy. It's $some_array[0]['filename']; Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 Wait, are these filenames stored in a database? Quote Link to comment Share on other sites More sharing options...
dijix316 Posted October 8, 2008 Author Share Posted October 8, 2008 I'm actually creating the file names from parts of the $users array. The $users array is derived from the text file which is tab delimited and uses underscores for spaces. Here's most of the code: <?php // tab delimited text file read into an array, text file also uses "_" for spaces. $users = file("10.08.08.txt"); for ($i = 0; $i < count($users); $i++) { // separate each element and store in a temporary array $tmp = str_replace("_", " ", explode(' ', $users[$i])); // assign each element of the temporary array to a named array key $users[$i] = array('link' => $tmp[0], 'casenum' => $tmp[1], 'claimant' => $tmp[2], 'adjuster' => $tmp[3], 'date' => $tmp[4], 'investigator' => $tmp[5], 'minutes' => $tmp[6], 'caseinfo' => $tmp[7], 'injury' => $tmp[8]); // Break up the text so that we can search for keywords // Need to figure out how to add number after initials are created if that investigator works more than one case. $initials = explode(" ", $users[$i]['investigator']); $initials = strtolower($initials[0]{0}).strtolower($initials[1]{0}); $fileDate = explode("/", $users[$i]['date']); $filename= $fileDate[0].'.'.$fileDate[1].'.'.$fileDate[2].$initials.'.wmv'; // Add named key to $users array $users[$i]['filename'] = $filename; $text = explode(" ", $users[$i]['caseinfo']); // Check if current month, previous month, title, or ? mark exists in the text if(in_array($curMonth, $text) || in_array($preMonth, $text) || in_array('?', $text) || in_array('Ms.', $text) || in_array('Mr.', $text) || in_array('Mrs.', $text)) { } else { $users[$i]['injury'] = $users[$i]['caseinfo']; } } ?> </p> <p> <form name="ndvInfo" method="post"> <table align="center" width="100%" cellspacing="6"> <tr> <th width="5%" align="left" scope="col">Link</th> <th width="7%" align="left" scope="col">Case#</th> <th width="9%" align="left" scope="col">Claimant</th> <th width="9%" align="left" scope="col">Adjuster</th> <th width="5%" align="left" scope="col">Date</th> <th width="11%" align="left" scope="col">Investigator</th> <th width="7%" align="left" scope="col">Minutes</th> <th width="47%" align="left" scope="col">Injury</th> </tr> <?php for ($i=0; $i<count($users); $i++) { ?> <tr> <td valign="top"><?php echo $users[$i]['link']; ?></td> <td valign="top"><input name="casenum" type="text" size="1" value="<?php echo htmlentities($users[$i]['casenum']); ?>" /></input></td> <td valign="top"><input name="claimant" type="text" size="15" value="<?php echo htmlentities($users[$i]['claimant']); ?>" /></input></td> <td valign="top"><input name="adjuster" type="text" size="5" value="<?php echo htmlentities($users[$i]['adjuster']); ?>" /></input></td> <td valign="top"><input name="date" type="text" size="7" value="<?php echo htmlentities($users[$i]['date']); ?>" /></input></td> <td valign="top"><input name="investigator" type="text" size="15" value="<?php echo htmlentities($users[$i]['investigator']); ?>" /></input></td> <td valign="top"><input name="minutes" type="text" size="1" value="<?php echo htmlentities($users[$i]['minutes']); ?>" /></input></td> <td valign="top"><textarea name="injry" id="injury" cols="40"><?php echo htmlentities($users[$i]['injury']); ?></textarea></td> <td valign="top"><input name="filename" type="hidden" value="<?php echo htmlentities($users[$i]['filename']); ?>" /></input></td> </tr> <?php } ?> </table> ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 Ah. But now, why would you want to just go around changing file names if those files potentially don't exist? Quote Link to comment Share on other sites More sharing options...
dijix316 Posted October 8, 2008 Author Share Posted October 8, 2008 The file name I'm creating has nothing to do with the file name of the text file the $users array is derived from. It's a .wmv file that will be manually named and uploaded by a video tech. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 Oh. Okay. So you have all of the file names in an array: <?php $filenames = array('test.wmv', 'test.wmv', 'test.wmv'); $good = array(); foreach ($filenames as $key => $fname) { $num = 0; $new_fname = $fname; while (array_key_exists($new_fname, $good)) { $file_parts = explode('.', $fname); $last = array_pop($file_parts); $new_fname = implode('.', $file_parts) . $num . ".$last"; $num++; } $good[$new_fname] = true; } print_r($good); Seemed to work for me. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2008 Share Posted October 8, 2008 example <?php $ar = array('a', 'a', 'a', 'b', 'c', 'c'); $k = array_count_values($ar); $new = array(); foreach ($k as $val => $count) { if ($count == 1) { $new[] = $val; } else { for ($i=1; $i <= $count; $i++) { $new[] = $val.$i; } } } echo '<pre>', print_r($new, true), '</pre>'; ?> --> Array ( [0] => a1 [1] => a2 [2] => a3 [3] => b [4] => c1 [5] => c2 ) Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 See, I was thinking along those lines too, but the thing is that he wants the number before the extension, so I found it easier just to pop it off, add the next number, and then add the extension back on. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2008 Share Posted October 8, 2008 it only needs a minor change to process with extensions <?php $ar = array('a.wmv', 'a.wmv', 'a.wmv', 'b.wmv', 'c.wmv', 'c.wmv'); $k = array_count_values($ar); $new = array(); foreach ($k as $val => $count) { if ($count == 1) { $new[] = $val; } else { for ($i=1; $i <= $count; $i++) { $new[] = str_replace ('.', "$i.", $val); // changed for use with extensions } } } echo '<pre>', print_r($new, true), '</pre>'; ?> --> Array ( [0] => a1.wmv [1] => a2.wmv [2] => a3.wmv [3] => b.wmv [4] => c1.wmv [5] => c2.wmv ) Quote Link to comment Share on other sites More sharing options...
dijix316 Posted October 8, 2008 Author Share Posted October 8, 2008 Thanks for the help...i'll check it out when i get a chance. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 The file is going to have more than one . based on the code he presented earlier. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2008 Share Posted October 8, 2008 Oh well, mine will just give a few extra nums but still unique. I still think array_count_values is the easiest way to go. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 9, 2008 Share Posted October 9, 2008 Oh well, mine will just give a few extra nums but still unique. I still think array_count_values is the easiest way to go. Yeah, seems that way. I hardly find myself using that function, even though it's actually pretty useful. 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.