...

View Full Version : Duplicates in an Array



muneeba9071
05-31-2011, 10:45 AM
Hi Guys,

I have an array:


$array = array('test','test2','test3','test3','test3','test4','test4');


I want to loop through that array and the same time find duplicates in the array and rename them, so I want the end result to be:

test
test2
test3
test3_2
test3_4
test4
test4_2

Many Thanks in advance for help.

bullant
05-31-2011, 11:27 AM
One option.


<?php

$myArray = array('test', 'test2', 'test3', 'test3', 'test3', 'test4', 'test4');
foreach ($myArray as $val) {
$count = 0;
$ext = 0;
for ($i = 0; $i < count($myArray); $i++) {
if ($val == $myArray[$i]) {
++$count;
if ($count > 1) {
$ext += 2;
$myArray[$i] = $val . '_' . $ext;
}
}
}
}
print_r($myArray);
?>

muneeba9071
05-31-2011, 11:31 AM
Many Thanks bullant, that's exactly what I was looking for. Thanks again

bullant
05-31-2011, 11:34 AM
you're welcome :)

muneeba9071
05-31-2011, 02:30 PM
Hi bullant,

I have another question, what if I have arrays within an array like:



$array[] = array('test','test2','test3','test3','test3','test4','test4');
$array[] = array('test','test2','test3','test3','test3','test4','test4');
$array[] = array('test','test2','test3','test3','test3','test4','test4');
$array[] = array('test','test2','test3','test3','test3','test4','test4');
$array[] = array('test','test2','test3','test3','test3','test4','test4');


I want to loop through $array and check it's arrays for duplicates, your above script works perfect for one array, can you please make it work with multiple-arrays.

Many Thanks

bullant
05-31-2011, 02:41 PM
Hi bullant,

I have another question, what if I have arrays within an array like:


You now have a 2D array and so you need an extra foreach loop.


<?php

$myArray = array();
$myArray[0] = array('test', 'test4', 'test3', 'test4', 'test3', 'test2', 'test3');
$myArray[1] = array('test', 'test3', 'test4', 'test3', 'test4', 'test3', 'test2');
foreach ($myArray as $key => $rowArray) {
foreach ($rowArray as $val) {
$count = 0;
$ext = 0;
for ($i = 0; $i < count($myArray[$key]); $i++) {
if ($val == $myArray[$key][$i]) {
++$count;
if ($count > 1) {
$ext += 2;
$myArray[$key][$i] = $val . '_' . $ext;
}
}
}
}
}

print_r($myArray);
?>

Fou-Lu
05-31-2011, 02:49 PM
This is actually easier using an array_walk.


function collisionSuffix(&$val, $key, &$used)
{
if (is_string($val))
{
if (isset($used[$val]))
{
++$used[$val];
}
else
{
$used[$val] = 1;
}

if ($used[$val] > 1)
{
$val = $val . '_' . $used[$val];
}
}
}

$array[] = array('test','test2','test3','test3','test3','test4','test4');
$array[] = array('test','test2','test3','test3','test3','test4','test4');
$array[] = array('test','test2','test3','test3','test3','test4','test4');
$array[] = array('test','test2','test3','test3','test3','test4','test4');
$array[] = array('test','test2','test3','test3','test3','test4','test4');
$aUsed = array();
array_walk_recursive($array, 'collisionSuffix', &$aUsed);
printf('<pre>%s</pre>', print_r($array, true));


Interesting enough, it only appears to work when the $aUsed is passed by reference at run-time (smells like a bug to me, as $used becomes a scalar value on the second iteration of the recursion). This will suffix all of your variables regardless of which offset they are in. If the intent is to just do each dimension, I'd use a foreach and walk each level through with just an array_walk instead of a recursive one.

bullant
05-31-2011, 03:00 PM
I want to loop through $array and check it's arrays for duplicates,.....

also, you haven't specified if you want to reset the offset counter $ext to 0 at each new row in the 2D array or to just continue incrementing it by 2 from the current value.

I tossed a coin and it came up as leaving it as the original code.

muneeba9071
05-31-2011, 03:09 PM
Hi Thanks to both of you, I really liked Fou-Lu's solution, Fou-Lu.. is it possible to disable the duplication check on certain array elements?

Thanks

Fou-Lu
05-31-2011, 03:17 PM
Hi Thanks to both of you, I really liked Fou-Lu's solution, Fou-Lu.. is it possible to disable the duplication check on certain array elements?

Thanks

I don't understand what you mean, can you show an example?

muneeba9071
05-31-2011, 03:19 PM
Let's say I have an array



array('name' => 'John', 'phone' =>'04578787');


I want to use your script, but I want to run the duplication on just the phone number

Fou-Lu
05-31-2011, 03:58 PM
Let's say I have an array



array('name' => 'John', 'phone' =>'04578787');


I want to use your script, but I want to run the duplication on just the phone number

There are a couple ways.
The $key within the function will contain the key for the item. You can change the code to accept only specifics for rename:


function collisionSuffixSpecified(&$val, $key, &$used)
{
if (is_string($val))
{
if (in_array($key, $used['specified'], true))
{
if (isset($used['used'][$val]))
{
++$used['used'][$val];
}
else
{
$used['used'][$val] = 1;
}
if ($used['used'][$val] > 1)
{
$val = $val . '_' . $used['used'][$val];
}
}
}
}

$array[] = array('test', 'phone' => 'test2', 'test3','test3','test3','test4','test4');
$array[] = array('test','test2','test3', 'phone' => 'test2','test3','test4','test4');
$array[] = array('test','test2','test3','test3', 'phone' => 'test3','test4','test4');
$array[] = array('test','test2','test3','test3','test3','test4','test4');
$array[] = array('test','test2','test3','test3','test3','test4','test4');

$aUsed = array(
'specified' => array('phone'),
'used' => array()
);
array_walk_recursive($array, 'collisionSuffixSpecified', &$aUsed);
printf('<pre>%s</pre>', print_r($array, true));


This does mean that you must use array_walk_recursive to span across all of the dimensions. You cannot otherwise have two keys of the same value within a single array.

muneeba9071
05-31-2011, 04:05 PM
That's it! Many Thanks!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum