0

I' ve fallen into endless foreach's.

I got an array:

$array[0] = array( A,B );
$array[1] = array( A,C );
$array[2] = array( B,C );
$array[3] = array( B,E );
$array[4] = array( C,E );

What I need to get is this:

$array[0] = array( A,B,C );
$array[1] = array( A,B,E );
$array[2] = array( A,C,E );
$array[3] = array( B,C,E );

Is there an easy way to accomplish this by a simple function? Also I need to create 4 value arrays of this results above: A,B,C,E etc.

7
  • Could you explain a little bit more precise, what you mean? 1. The alphabet we know looks more like 'ABCD' and then for your first part AB,AC,BC,BE,CE look a bit strange to me, like you have forgotten AE... Commented Apr 6, 2012 at 22:41
  • It don't think it matter what the letters are. He just wants the cartesian product of the unique array elements. Commented Apr 6, 2012 at 22:45
  • Looks like you missed A,E pair in the first part. Otherwise I see no logic in the way you organize them. @webbiedave No, it is not a cartesian product. It is a permutation list. Commented Apr 6, 2012 at 22:46
  • it is not alphabet in there, treat letters as numbers A=1,B=2 and so on. I need to create patterns from first example ( 3 item ones ) and from 3 item arrays I need to create 4 item arrays.. and so on Commented Apr 6, 2012 at 22:47
  • Is this homework? There's a homework tag you need to put on there. Commented Apr 6, 2012 at 22:53

3 Answers 3

1

Something like this (not tested):

$out = array();
foreach($array as $first) {
    foreach($array as $second) {
        if (array_slice($first, 1) == array_slice($second, -1)) {
            $copy = $first;
            $last = array_slice($second, -1, 1);
            $copy []= $last[0];
            $out []= $copy;
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for this, but as Kerwindena code, this would only for for creating 3 item long arrays. I already have messy code for making 2 item long arrays. Thats why I am trying to make function for that.
No, this is a general code for 2->3, 3->4, etc generation. For any step.
Oh.. sorry than I didn't get it. I will try it now.
This is almost perfect.. changed == statement on != and it gives good results but some of them are the same like A,B,C and A,C,B ( it is the same for this case ).So now I have to sort it and replace duplicated arrays. Thank you for your effort!
if you sort the input, and every input is only once present, each output will be sorted an also only once present...
|
1

If you need to generate array of unique permutations, you can use code from this article:

http://www.dannyherran.com/2011/06/finding-unique-array-combinations-with-php-permutations/

1 Comment

Thank you for this link but what I need to make is not strictly permutations, it is joining similar arrays into wider one.
0

What about:

$array[0] = array( A,B );
$array[1] = array( A,C );
$array[2] = array( B,C );
$array[3] = array( B,E );
$array[4] = array( C,E );

$array2 = new array();

for($i = 0; $i < count($array); $i++)
{
    for($j = 0; $j < count($array); $j++)
    {
        if($array[$i][1] == $array[$j][0])
        {
            $array2[] = new array($array[$i][0], $array[$i][1], $array[$j][1]);
        }
    }
}

1 Comment

thanks, this would work but only for making 3 item long arrays, but than I need to match 2 first items to create 4 ones. That would require like 2 more if statements - too complicated if there were 5 item ones possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.