...

View Full Version : get duplicate values from an array and reuse



clunky
08-28-2007, 03:11 PM
Hi
Can anyone help with getting any duplicate values out of an array and build another array out of them?

Any pointers would be really helpful, I have tried array unique and a few others but cannot identify the dupes and get them out.
cheers ;-)

Inigoesdr
08-28-2007, 03:28 PM
function array_duplicates($array)
{
if(!is_array($array))
return false;
$duplicates = array();
$unique = array_unique($array);
if(count($array) > count($unique))
for($i = 0; $i < count($array); $i++)
if(!array_key_exists($i, $unique))
$duplicates[] = $array[$i];
return $duplicates;
}

$a = array('1', 1, 2, 3, 4, '4', 5, '5');
$duplicates = array_duplicates($a);
$a = array_unique($a);

Mwnciau
08-28-2007, 06:45 PM
$newarray = array_unique(array_diff($array, array_unique($array)));

Inigoesdr
08-28-2007, 07:26 PM
$newarray = array_unique(array_diff($array, array_unique($array)));

Did you try that code?

Mwnciau
08-28-2007, 07:47 PM
Nope, I didn't realise that array_diff didn't find the difference including duplicates.

clunky
08-29-2007, 11:01 AM
thanks guys giving it a go now.... ;-)

OK so far..........
The basis of this function works just fine.
New problem is to get all duplicates out as individual values so that they can be used to update individually.

I have managed to use the above function to some extent to loop ( I use that term rather loosely) through the array in order to test to see if there are any further unique values and extract them also.

The problem is that there could ba any number of duplicates in the array and I don't really want to write lines and lines of code to keep extracting.
There must be an easier way than the one I have got .
Lets say that the user inputs 10 values, each one is needed to update db fields so duplicates won't allow this.
If the user has 9 dupes then each of the 9 has to be extracted somehow in order to run the update.

$prod = $_REQUEST[stock_in];//input from form

$wordChunks = explode("\r\n", $prod);//explode at the end of item name /
trim($wordChunks);
///////////////////////////////////////////function for duplicates
function array_duplicates($array)
{
if(!is_array($array))
return false;
$duplicates = array();
$unique = array_unique($array);
if(count($array) > count($unique))
for($i = 0; $i < count($array); $i++)
if(!array_key_exists($i, $unique))
$duplicates[] = $array[$i];
return $duplicates;
}

$a = $wordChunks;
$duplicates = array_duplicates($a);
$a3 = array_unique($a);

$wordChunksD = implode("','", $duplicates);
echo "dupes = ".$wordChunksD."<br>";

echo"==============<br>";

$wordChunks2 = implode("','", $a3);
echo "unique = ".$wordChunks2."<br>";

echo"==============<br>";
//////////////////////////////// second time around//////////////////////////

$wordChunksE = explode("','", $wordChunksD);//explode at the end of item name /
trim($wordChunksE);
$ab = $wordChunksE;
$ax = array_unique($ab);
$duplicates2 = array_duplicates($ab);

$wordChunksE = implode("','", $duplicates2);
echo "next dupes = ".$wordChunksE."<br>";

echo"==============<br>";
$wordChunksF = implode("','", $ax);
echo "next unique = ".$wordChunksF."<br>";
Is it possible to do this from within a loop?
i.e. check for dupes, extract the unique ones, update them, check for dupes in the new array, extract them and update the unique ones and so on until all have been updated???

Hope you can help me.
Cheers

clunky
08-30-2007, 11:35 AM
Anyone please?

Mwnciau
08-31-2007, 12:22 PM
function getdupes($array){
if (!is_array($array)) return false;
$found = array();
foreach($array as $key => $value){
if (!in_array($value, $found)){
unset($array[$key]);
$found[] = $value;
}
}
return $array;
}
function hasdupes($array){
if (!is_array($array)) return false;
$found = array();
foreach($array as $key => $value){
if (!in_array($value, $found)){
unset($array[$key]);
$found[] = $value;
} else {
return true;
}
}
return false;
}

$array = array( ... );

while (hasdupes($array)){
$array = getdupes($array);
$dupes = array_unique($array);

// $dupes contains all the dupes once only

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum