Jump to content

Recommended Posts

Hi all,

 

Im struggling with arrays.Say I have two arrays:

 

array1 = array('one','two','three','four','five');

array2 = array('a','b','c','d');

 

If I want to choose random combination of these two arrays, but want to keep the combinations unique, for example I have 'one' from array1 and 'c' from array2, but I want many combinations... but not the same as 'one' from array1 and 'c' from array2...does anyone know how to do this??

Link to comment
https://forums.phpfreaks.com/topic/165837-2-arrays/
Share on other sites

If I want to choose random combination of these two arrays, but want to keep the combinations unique,

 

To ensure the results are unique you could either create a new array of all the combinations or create a process that will keep track of the previous combinations and ensure there are no duplicates. The process I would use would depend on how big I expect those two arrays to be and how many combinations I expect to retrieve

 

Link to comment
https://forums.phpfreaks.com/topic/165837-2-arrays/#findComment-874779
Share on other sites

The following function will return a multidimentional array of every combination between array1 and array2. You could then do an array_rand to radomize the order of the combinations. Then get however many unique pairs that you need.

function arrayJoin($array1, $array2)
{
    $combinedArray = array();
    foreach ($array1 as $value1)
    {
        foreach ($array2 as $value2)
        {
            $combinedArray[] = array($value1, $value2);
        }
    }
    return $combinedArray;
}

Link to comment
https://forums.phpfreaks.com/topic/165837-2-arrays/#findComment-874794
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.