-5

i got the result my for loop :
FFFF
AAAA
TTTT
EEEE

while mod=4 my loop echo br

but i want result like this :
FATE
FATE
FATE
FATE

how i do this in php ? thanks for answers and sorry my english:D

  • 4
    Welcome to Stack Overflow. Please post some of your code. What have you tried? It's difficult to offer advice without more context like – Milk Aug 14 '17 at 01:07
  • Please improve the quality of your question while you are here. Please show how the inout data is structured. Is it a string? Is it an array of strings? – mickmackusa Aug 14 '17 at 11:14
  • Please edit your question. – mickmackusa Aug 14 '17 at 11:18

1 Answers1

0

This is almost a duplicate of:

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);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Good on you typing this up given 0 info from the TO. There is no mention of `array` in the question though - the question relates to a `string`... ? – trs Aug 14 '17 at 02:34
  • The OP's unrotated result is from a for loop. If it is not from an array, then it is from something acting like an array and is most sensibly converted to an array. – mickmackusa Aug 14 '17 at 02:40
  • Thank you mickmakusa and thanks for your comments ! – user8459759 Aug 14 '17 at 11:16
  • @user8459759 Other people are disappointed that I answered your low quality question. Please provide more details in your question. – mickmackusa Aug 14 '17 at 11:20