Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding two arrays without dupes

Status
Not open for further replies.

lvennard

MIS
Apr 20, 2000
93
US
For some reason, this doesnt work. Can someone look at it with fresh eyes and tell me what im doing wrong.

I want to add two arrays and eliminate all duplicates.
an example, array1="a, b, c, d"
array2="a, b, e, f"
Then
Addarray="a, b, c, d, e, f"


here is my code. The add part works, but I continue to get the dupes.


//====================================================
//========= Compile Arrays into One =========
//====================================================
function compilelist($list1, $list2) {
$numlist1 = count($list1);
$numlist2 = count($list2);
$x = 1;
$y = 1;
$counter = $numlist2+1;
$compiled = $list2;
while ($x <= $numlist1) {
while ($y <= $numlist2) {
if ($list1[$x] == $list2[$y]) {
$add = &quot;no&quot;; }
$y = $y + 1;
}
$y = 1;
if (!$add) {
$compiled[$counter] = $list1[$x];
$counter = $counter + 1;
unset ($add); }
$x = $x + 1;
}
return ($compiled);
} // End of Compile List Func
 
As simple as this:
$newarray=array_unique(array_merge($array1,$array2,...,$arrayN));

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Fatal error: Call to unsupported or undefined function array_unique()

Im still using PHP3.
Would have worked though if i had been on php4.

 
yes, it works in the latest version.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top