Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@E1101
Created January 5, 2019 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save E1101/8dba82a92fc40be42777d2d0ddcade7f to your computer and use it in GitHub Desktop.
Save E1101/8dba82a92fc40be42777d2d0ddcade7f to your computer and use it in GitHub Desktop.
<?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