Combine delimited values from multiple arrays to form a transposed string of delimited values [duplicate]

StackOverflow https://stackoverflow.com/questions/20682173

  •  19-09-2022
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top