Hi
I have two array
$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.
First, make them associative arrays (faster way to do this anyone?)
foreach($a as $idx=>$aValue) { $c[$aValue] = $b[$idx]; } Then, get rid of copies. foreach($c as $idx=>$cVal) { if(!isset($d[$idx])) { $d[$idx] = $cVal; } }
Now $d holds an associative array where the first set is pointing to the second set...
Jason
Well, you are on the right track, but I would just like to point out that the foreach loop is unnecessary because when, in your example, you are creating the array $c, any duplicates will have the same key, and thus merely overwrite the value before it, eliminating any duplicates in the process. Just trying to keep it simple 🙂
Hope that helps!
Chris King
Hehe, oops!
Thanks for that catch. It must have been kinda late...
~Jason