I didn't like that the given solution is modifying an iterated variable within its loop and also I wanted a solution that I can easily understand to port to other languages. So here:
<?php
function permutation(array $arr)
{
        $out=[[]];
    
        foreach($arr as $key2=> $item2){
            $copy=$out;
            foreach($copy as $k=> $v){
                array_push($copy[$k],$item2 );
            }
            array_push($out,...$copy);
            
        }
        
        return $out;
}
print_r(permutation(array(1,2,3,4)));
This second one is intentionally weird to make it better to understand what is going on.
<?php
function permutation(array $arr)
{
    
    $out=[];
    
    while(count($arr)){
        
        $out_temp=[[]];
    
        foreach($arr as $key2=> $item2){
            $copy=$out_temp;
            foreach($copy as $k=> $v){
                array_push($copy[$k],$item2 );
            }
            if($key2==0){
                unset($out_temp[0]);
            }
            array_push($out_temp,...$copy);
 
        }
        
        array_push($out,...$out_temp);
        array_shift($arr);
    }
    return $out;
}
print_r(permutation(array(1,2,3,4,5)));