0

I have two string arrays.

Array Colors = { Blue, Green, Yellow, Red }

Array Toys = { Balloon, Whistle, Ball }

I want to concatenate these two arrays and display the output in such a way that, the result will look like this:

BlueBaloon
BlueWhistle
BlueBall
GreenBaloon
GreenWhistle
GreenBall
YellowBaloon
YellowWhistle
YellowBall
RedBaloon
RedWhistle
RedBall

Any help would be greatly appreciated. Thanks.

1
  • What kind of syntax should this be? Commented May 5, 2014 at 7:29

4 Answers 4

1

Your syntax isn't php standard, however ....

$arrayColors = array('Blue', 'Green', 'Yellow', 'Red');
$arrayToys = array('Balloon', 'Whistle', 'Ball');

foreach($arrayColors as $color) {
 foreach($arrayToys as $toy) {
   echo $color.$toy.'<br/>';
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Just loop through both arrays. And push it into another one.

$newArray = array();

foreach($colors as $color) {
   foreach($toys as $toy) {
      $newArray[] = $color.$toy;
   }
}

Comments

0

Untested:

//Loop through each color
foreach($Colors AS $color)
{
    //Now loop through each toy
    foreach($Toys AS $toy){
        //Now we can concatenate each toy with each color
        $toyColor = $color.$toy;
        echo $toyColor;
    }
 }

Comments

0

a simple foreach would do this for you.

$Colors =['Blue', 'Green', 'Yellow', 'Red'];

$Toys = ['Balloon', 'Whistle', 'Ball'];

foreach($color in $Colors){
    foreach($toy in $Toys){
       echo $color.$toy;
    }
}

Comments

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.