If you want to use substr
function str2array($string) {
$strArray = [];
for ($i = 0; $i < strlen($string); $i++) {
$strArray[] = substr($string, $i, 1);
}
return $strArray;
}
But use str_split
instead.
$string = 'tone';
$strArray = str_split($string);
foreach ($strArray as $key => $char1) {
$strArray2 = $strArray;
unset($strArray2[$key]);
foreach ($strArray2 as $char2) {
$permutations[] = $char1 . $char2;
}
}
//EDIT: adding array_unique in case of dupes
$uniquePermutations=array_unique($permutations);
print_r($uniquePermutations);
Array
(
[0] => to
[1] => tn
[2] => te
[3] => ot
[4] => on
[5] => oe
[6] => nt
[7] => no
[8] => ne
[9] => et
[10] => eo
[11] => en
)