在PHP中连接两个字符串数组

时间:2014-05-05 07:26:52

标签: php arrays string function concatenation

我有两个字符串数组。

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

Array Toys = { Balloon, Whistle, Ball }

我想连接这两个数组并以这样的方式显示输出,结果将如下所示:

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

非常感谢任何帮助。感谢。

4 个答案:

答案 0 :(得分:1)

你的语法不是php标准,但是......

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

foreach($arrayColors as $color) {
 foreach($arrayToys as $toy) {
   echo $color.$toy.'<br/>';
 }
}

答案 1 :(得分:0)

只需遍历两个数组。把它推到另一个。

$newArray = array();

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

答案 2 :(得分:0)

未测试:

//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;
    }
 }

答案 3 :(得分:0)

一个简单的foreach会为你做这件事。

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

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

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