Created
January 5, 2019 17:32
-
-
Save E1101/8dba82a92fc40be42777d2d0ddcade7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Reverse Words In Array Of Charachters | |
* | |
* @param array $input | |
* | |
* @return array | |
*/ | |
function reverseWords(array $input) : array | |
{ | |
$tWord = []; | |
foreach ($input as $index => $c) | |
{ | |
if ( ord($c) == 32 || $flag = ($index == sizeof($input)-1) ) | |
{ | |
if ($flag) { | |
$tWord[] = $c; | |
$i = $index; | |
} else { | |
$i = $index-1; // replace at index before space appeared | |
} | |
while ( $t = array_pop($tWord) ) { | |
$input[$i-count($tWord)] = $t; | |
} | |
$tWord = []; | |
continue; | |
} | |
$tWord[] = $c; | |
} | |
return $input; | |
} | |
print_r( | |
reverseWords(['I', ' ', 'l','o','v','e',' ','T','a','x','i','f','y']) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment