Help with arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rfeio
Forum Newbie
Posts: 15
Joined: Fri Aug 22, 2008 4:23 am

Help with arrays

Post by rfeio »

Hi!

I have this script that generates a multiple array: array[x][y] Depending on the user input the array can be 'x' can be 1 or more.

I want to use the array[x] with the array_intersect as in array_intersect($array[0], $array[1], ...) So I thought of doing:
for($k=0; $k < count($array); $k++)
{
$temp .= '$array['.$k.'],';
} and then issuing:
array_intersect($temp) but obviously it doesn't work.

So my question is, how can I do this?

Cheers!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help with arrays

Post by Christopher »

You say that you "want to use the array[x] with the array_intersect", but you don't say what you want to do with the arrays? Can you show what the resulting array should look like?
(#10850)
rfeio
Forum Newbie
Posts: 15
Joined: Fri Aug 22, 2008 4:23 am

Re: Help with arrays

Post by rfeio »

Well, the idea is to check the values that are common to all the arrays.

For example let's suppose that:

array[1] has the values (a, b, c, d, e)

array[2] has the values (a, e, f, g, h)

array[3] has the values (e, f, g, h, k)

The end result should be 'e' which is common to all the 3 arrays.

Now one of the challenges is that I don't know how many arrays I will have in the beginning. In this example it was 3, but it can be more or less then that.

Any ideas?
rfeio
Forum Newbie
Posts: 15
Joined: Fri Aug 22, 2008 4:23 am

Re: Help with arrays

Post by rfeio »

Ok, merging the arrays in one single array with array_merge() and then filtering the results with array_unique() solved my problem.
Post Reply