Jump to content

2 Arrays


common

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.