Reputation: 1
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.
Upvotes: 0
Views: 90
Reputation: 1771
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;
}
}
Upvotes: 0
Reputation: 2208
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;
}
}
Upvotes: 0
Reputation: 29932
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/>';
}
}
Upvotes: 1
Reputation: 12246
Just loop through both arrays. And push it into another one.
$newArray = array();
foreach($colors as $color) {
foreach($toys as $toy) {
$newArray[] = $color.$toy;
}
}
Upvotes: 0