Jump to content

Help with arrays


rfeio

Recommended Posts

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!

Link to comment
Share on other sites

Just to explain the objective of this exercise:

 

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?

Link to comment
Share on other sites

Yeap, DJ Kat's solution seems a good one.

 

Now, how can I solve my other problem? I don't know how many arrays I have to begin with in order to merge them in one single array.

 

My idea was to create a variable with the arguments for the array_merge() function like:

 

for($i=0; $i < $max_num_arrays; $i++)

{

  $arguments .= '$array['.$i.'],';

}

 

and then: array_merge($arguments)

 

But this doen's work because $arguments is a string and not an array.

 

Any ideas?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.