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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Problem

Status
Not open for further replies.

OOzy

Programmer
Jul 24, 2000
135
SA
Hi

I have two arrays

$a=(3, 44, 5, 6, 6, 7);
$b=(a, b, q, r, r, d);

each number in the first array corresponds to a letter in the 2nd array. how can I get rid of similar terms. For example 6 and r are double. I want to get rid of one pair.
 
The array_unique function is good if you have dupes in the same array. If you want to compare 2 different arrays, you can do that like this:

<?php
foreach($a as $c)
{foreach($b as $d)
{if($c == $d)
unset($d);}
}
?>

The problem with unset tho...is that it will remove the indices/keys of the array element (unlike the array_unique). For example (these variables are not corresponding to the above code): If $a's index is 0, and $b is 1 (in the first array)....$c is 0 and $d is 1 in the (in the second array)....and $d & $a are dupes in the 2 arrays...you unset $a, it then $a would be gone and $c will remain 1. The indices do not change!
- PAINKILLER
 
aack...last line: you unset $a and then $a would be gone and $b would remain with an index of 1. There would be no 0 in the first array. Ofcourse this just applies to 1 dimensional arrays.

Hope that helped, it's fairly simple. :) - PAINKILLER
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top