0

Is there any method to get the union of two or more arrays?

example:

$array1 = [x1, x2, x3];

$array2 = [y1, y2, y3];

// and the result will be 

$array2 = [x1 y1, x1 y2, x1 y3, x2 y1, x2 y2 ,.....];
6
  • Yes, there are many ways to do this. Have you tried anything so far? Commented Oct 27, 2014 at 10:58
  • 1
    What does x1 y1 mean? Concatenation? Addition? Commented Oct 27, 2014 at 10:59
  • 1
    what is it multiplicatrion Commented Oct 27, 2014 at 11:00
  • check this question Commented Oct 27, 2014 at 11:07
  • possible duplicate of PHP append one array to another (not array_push or +) Commented Oct 27, 2014 at 11:08

1 Answer 1

2

This should work for you:

<?php 

    $array1 = array("x1", "x2", "x3"
    $array2 = array("y1", "y2", "y3");

    $array3 = array();

    // and the result will be   
    //$array3 = [x1 y1, x1 y2, x1 y3, x2 y1, x2 y2 ,.....];


    for($count = 0; $count < count($array1); $count++) {

        for($countArrayTwo = 0; $countArrayTwo < count($array2); $countArrayTwo++)
            $array3[]  = $array1[$count] . " " . $array2[$countArrayTwo];

    }

    print_r($array3);

?>
Sign up to request clarification or add additional context in comments.

2 Comments

what is this doing ?
@danielad it combines both arrays as OP requested

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.