0

i want combine 2 array like this

Example 1 :

Arr1 = ['A','B','C'],
Arr2 = ['D','E']

will be become

Arr3 = [
  ['A','D'],['A','E'],['B','D'],['B','E'],['',''] ....
]

Example 2 :

Arr1 = ['A','B','C'],
Arr2 = ['D','E']
Arr3 = ['G','H']

will be become

Arr4 = [
  ['A','D','G'],['A','E','G'],['','',''].... 
]

Any idea or suggest me algorithm can be like this thanks so much

5
  • Two foreach loops. Your second example (also named Ex1) is not detailed enough for us to know what is happening. Commented Sep 20, 2017 at 3:08
  • 2
    Sounds like you're looking for the cartesian product of two arrays. Perhaps this answer could help? Commented Sep 20, 2017 at 3:11
  • This my answer stackoverflow.com/questions/6311779/… thanks so much my friend @tugayac Commented Sep 20, 2017 at 3:24
  • No worries, glad it was useful :) Commented Sep 20, 2017 at 3:26
  • Why the php tag? Commented Sep 20, 2017 at 5:52

2 Answers 2

1

Simplified version; merging array by index

$arr1 = range('a', 'b');
$arr2 = range('c', 'f');
$arr3 = range('g', 'k');
$arr4 = range('x', 'z');
$res  = array();

// $counter = 1;
// while ($counter <= 4) {
//   $array = "arr{$counter}";
//   funcIndexMerge($res, $$array);
//   $counter++;
// }

funcIndexMerge($res, $arr1);
funcIndexMerge($res, $arr2);
funcIndexMerge($res, $arr3);
funcIndexMerge($res, $arr4);
var_export($res);

function funcIndexMerge(&$res, $array) {
    foreach ($array as $ari => $val)  {
        if (!isset($res[$ari])) {
            $res[$ari] = array();
        }
        $res[$ari] = array_merge($res[$ari], array($val));
    }
 }
Sign up to request clarification or add additional context in comments.

Comments

1

The problem you suggest is very similar to this Leetcode Challenge. The idea is to use Backtracking

Psuedo PsedoCode :

result := string Array
CartesianProduct(rowIndex, ArrayList, stringSoFar):

if rowIndex equal ArrayList.size:
    Add stringSoFar to result
    return
for(eachItem in ArrayList[rowIndex])
    CartesianProduct(rowIndex +1 , ArrayList, stringSoFar + eachItem)
    return 

This is a workhorse which does all the computation and can be called like CartesianProduct(0, list of Array to be multiplied, "")

Suppose your ArrayList = [['A'], ['C', 'D']]. And CP be CartesianProduct

                 CP(0, AL, "")
            (Take 'A')/
                     /
               CP(1, AL, "A") (stringSoFar becomes 'A')
         (Take C) /       \(Take 'D'.Second Iteration with second array['C', 'D']) 
                 /         \
         CP(2, AL, "AC")  CP(2, AL, "AD")
              /              \
  rowIndex equal size.   rowIndex equals listSize i.e No more list to look
  Add "AC" and return    Add stringsoFar ("AD") to result and rerturn 

My solution to the Leetcode problem(but in C++). Hopefully it'll give you some idea to write in PHP

class Solution {
public:
    map<char, vector<string>> values {
    {'2', vector<string>{"a", "b", "c"}},
    {'3', vector<string>{"d", "e", "f"}},
    {'4', vector<string>{"g", "h", "i"}},
    {'5', vector<string>{"j", "k", "l"}},
    {'6', vector<string>{"m", "n", "o"}},
    {'7', vector<string>{"p", "q", "r", "s"}},
    {'8', vector<string>{"t", "u", "v"}},
    {'9', vector<string>{"w", "x", "y", "z"}}
    };
vector<string> answer;
void doComb(int index, string digits, string sofar)
{
    if(index == digits.size())
    {
        if(sofar != "")
        {
            answer.push_back(sofar);    
        }
        return;
    }
    for(auto lett : values[digits[index]])
    {
        doComb(index + 1, digits, sofar + lett);
    }
    return;
}
vector<string> letterCombinations(string digits) {
    doComb(0, digits, "");
    return answer;
    }
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.