2013-12-19 41 views
0

我想的四根弦(用逗號分隔)的話混入一個字符串,如下所示:混合多個PHP串

$string1 = "apple, banana, grape"; 
$string2 = "red, yellow, black"; 
$string3 = "north, south, east"; 
$string4 = "april, may, june"; 

$output_string = "apple, red, north, april, banana, yellow, south, may, grape, black, east, june"; 

關於如何做到這一點任何想法?

+0

可能重複http://stackoverflow.com/questions/11441369/php -string-concatenation) – siannone

+0

元素的順序是否重要?如果沒有:http://stackoverflow.com/questions/11441369/php-string-concatenation – Leonardo

回答

0

試試這個(但這並不是最優化的好辦法)如果要素的順序是物質

$string1 = "apple, banana, grape"; 
    $string2 = "red, yellow, black"; 
    $string3 = "north, south, east"; 
    $string4 = "april, may, june"; 

$piece1 = explode(",", $string1); 
$piece2 = explode(",", $string2); 
$piece3 = explode(",", $string3); 
$piece4 = explode(",", $string4); 


$i = 0; 
foreach($piece1 as $element){ 
    $resultArray[] = $element; 
    $resultArray[] = $piece2[$i]; 
    $resultArray[] = $piece3[$i]; 
    $resultArray[] = $piece4[$i]; 
    $i++; 
} 

var_dump(implode(",", $resultArray)); 

結果,使用:

string(75)「apple,red,north,april,ba納納,黃,南,五月,葡萄,黑,東,六月「

編輯。更短的方式:

$i = 0; 
foreach($piece1 as $element){ 
    $resultArray.= $element.",".$piece2[$i].",".$piece3[$i].",".$piece4[$i]; 
    $i++; 
} 
var_dump($resultArray); 

結果將是相同的

+0

這正是我試圖完成。是的,這些要素的順序很重要。非常感謝你! – user3010407

+0

已編輯。我寫了更短的例子,可以使用這個。 – sergio

+0

@ user3010407:如果您有'$ stringX's,它有不同數量的拼接單詞,這個答案將會返回警告,並且額外的逗號將會出現在拼接的字符串中[請參見demo here](https:// eval 。在/ 81455)。在這種情況下,你可以像[this]一樣使用[my function](http://stackoverflow.com/a/20685445/67332)(https://eval.in/81450)。但是,如果你百分之百肯定,你的字符串總是有相同的字數,這是可以的。 –

0

我認爲你可以使用下面的代碼。

$totalString = implode(', ', array($string1, $string2, $string3, $string4)); 
$splittedValues = explode (',', $totalString); 

// trim all values 
$result = array_map('trim', $splittedValues); 

// randomize all values 
$result = shuffle($result); 

// merge it to a string 
$output_string = implode(', ', $result); 

古德勒克,

斯特凡

+0

好吧,你不覺得這會考慮每個陣列在一次,模式會不同於問什麼 – dreamweiver

0
$string1 = "apple, banana, grape"; 
$string2 = "red, yellow, black"; 
$string3 = "north, south, east"; 
$string4 = "april, may, june"; 

$myArray = array(
    explode(',', $string1), 
    explode(',', $string2), 
    explode(',', $string3), 
    explode(',', $string4), 
); 

$transposedData = call_user_func_array(
    'array_map', 
    array_merge(
     array(NULL), 
     $myArray 
    ) 
); 

array_walk(
    $transposedData, 
    function(&$value) { 
     $value = implode(', ', $value); 
    } 
); 
echo implode(',', $transposedData); 
0

您可以創建功能,將採取多種字符串,返回隨機字符串:

function mix_multiple_strings() { 
    $strings = array(); 
    foreach (func_get_args() as $string) { 
     $strings = array_merge($strings, explode(', ', $string)); 
    } 
    shuffle($strings); 
    return implode(', ', $strings); 
} 

echo mix_multiple_strings($string1, $string2, $string3, $string4); 

demo

或功能將在您的順序排列的字符串:

function arrange_multiple_strings() { 
    $strings = array(); 
    $n = func_num_args(); 
    foreach (func_get_args() as $i => $string) { 
     foreach (explode(', ', $string) as $j => $val) { 
      $strings[$n * $j + $i] = $val; 
     } 
    } 
    ksort($strings); 
    return implode(', ', $strings); 
} 

echo arrange_multiple_strings($string1, $string2, $string3, $string4); 

demo

[PHP字符串連接(的