Jump to content

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
https://forums.phpfreaks.com/topic/201689-help-with-arrays/
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
https://forums.phpfreaks.com/topic/201689-help-with-arrays/#findComment-1058183
Share on other sites

There is prob more then one way to solve this but here is an idea.

Why not put all the values in one array and use array_unique to remove the duplicate values.

That way you have all possible values stored in 1 array. Then do a check for each possible value if it exists in all the arrays you want to check

Link to comment
https://forums.phpfreaks.com/topic/201689-help-with-arrays/#findComment-1058197
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
https://forums.phpfreaks.com/topic/201689-help-with-arrays/#findComment-1058211
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.