Jump to content

Array with duplicate values to array with unique values?


dijix316

Recommended Posts

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.

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.

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>
?>

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.

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
)

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
)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.