tags:

views:

36

answers:

3

Hi guys,

I have two arrays

one like

1 | 1st name
2 | 2nd name
3 | 3rd name
4 | 4th name

and one like

1st name
2nd name
3rd name
4th name
5th name
6th name
7th name

Basically I want to intersect these arrays

I need to interesect these arrays so I end up with the second array being just

5th name
6th name
7th name

How would I go about doing this?

+1  A: 

What you have described looks more like diff. If values of your array look exactly like you described, you'll have to parse it into temp array first, so you end up with the exact form of data in both of the arrays you want to diff.

Ondrej Slinták
yes array_diff was the function I was looking for :)and yeah, I had to do that too.
Hailwood
A: 

if both array are already sorted it's a simple job to do: search "i" element of the second array in the first one, while the "j" element in the firt is "smaller" (have a look at strcmp). If you find it, then you go to next "i" element, if you don't put your "i" element in a third array. Then go on to next "i"; If it's not sorted maybe you can think of sorting it first; if you choose not to sort them, you must do a linear search of each item in array 2 in array 1.

I'm not shure about how your array are. If they are exactly as you wrote you must first parse them as Ondrej said.

ArtoAle
A: 

Actually, you can use the array_diff function that's native to PHP:

http://php.net/manual/en/function.array-diff.php

Jason Huebel