[Solved] PHP Vertical String to Horizontal String [closed]


This is almost a duplicate of:

  • How to restructure multi-dimensional array with columns as rows? and
  • Combining array inside multidimensional array with same key

This can be done with foreach loops, but I like the condensed variadic method (PHP 5.6+).
You can research the aforementioned links to see the other techniques if your version isn’t high enough or you want something different.

The strings just need to be converted to arrays before rotating, then imploded() after rotating.

Code: (Demo)

$input=['FFFF','AAAA','TTTT','EEEE'];
$rotated=array_map(function(){return implode(func_get_args());},...array_map('str_split',$input));
var_export($rotated);

Output:

array (
  0 => 'FATE',
  1 => 'FATE',
  2 => 'FATE',
  3 => 'FATE',
)

Here is a less fancy method to achieve the same result:

$input=['FFFF','AAAA','TTTT','EEEE'];
$length=strlen($input[0]);
foreach($input as $string){
    for($offset=0; $offset<$length; ++$offset){
        if(!isset($rotated[$offset])){$rotated[$offset]='';}
        $rotated[$offset].=$string[$offset];
    }
}
var_export($rotated);

4

solved PHP Vertical String to Horizontal String [closed]