0

I'm trying to mix the words of four strings (separated by a comma) into one string, as follows:

$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";

Any ideas on how to do it?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Does the order of the elements matter? If not: http://stackoverflow.com/questions/11441369/php-string-concatenation – Leonardo Dec 19 '13 at 12:53
  • As shown on the dupe target: [`echo implode(', ', array_merge(...array_map(null, explode(', ', $string1), explode(', ', $string2), explode(', ', $string3), explode(', ', $string4))));`](https://stackoverflow.com/a/69114863/2943403) – mickmackusa Sep 07 '22 at 04:41

4 Answers4

0

I think you can use the code below.

 $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);

Goodluck,

Stefan

Stefan Koenen
  • 2,289
  • 2
  • 20
  • 32
  • well, dont you think this would consider each array at a time,the pattern would be different than what is asked – dreamweiver Dec 19 '13 at 12:52
0

Try this ( but this is not good way of optimizing), use this if order of elements is matter

 $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));

Result:

string(75) "apple,red,north,april, banana, yellow, south, may, grape, black, east, june"

Edited. More shorter way:

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

Result will be the same

sergio
  • 5,210
  • 7
  • 24
  • 46
  • @user3010407: if you have `$stringX`'s, that have different count of concated words in it, this answer will return warnings and extra commas will be in the concated string, [see demo here](https://eval.in/81455). In that case you can use [my function](http://stackoverflow.com/a/20685445/67332) like [this](https://eval.in/81450). But, if you are 100% percent sure, you always have same number of words in all your strings, this will do. – Glavić Dec 20 '13 at 12:05
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);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

You can create function that will take in multiple strings, and return randomized string:

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

Or function that will arrange strings in your order:

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

Glavić
  • 42,781
  • 13
  • 77
  • 107