3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $str = array('A','B','A'); permute($str, 0, count($str)); function permute($arr, $i, $n) { if ($i == $n) { printArray($arr); return; } for ($j = $i; $j < $n; $j += 1) { swapArrayValues($arr, $i, $j); permute($arr, $i + 1, $n); swapArrayValues($arr, $i, $j); } } function swapArrayValues(&$array, $i, $j) { $tmp = $array[$i]; $array[$i] = $array[$j]; $array[$j] = $tmp; } function printArray($arr) { echo implode($arr, ' ') . "\n"; }
Output for 4.3.0 - 7.1.0
A B A A A B B A A B A A A B A A A B