rotate elements of an array with a fixed position

1

I have this array

$a = Array("a","b","c","d");

I want to rotate b, c, d. That is, B is placed where d and the position is lowered.

Example:

abcd
acdb
adbc

I've been here for the moment.

$a = Array("a","b","c","d");
print_r($a);
$num = count($a)-1;

for($j=0;$j<3;$j++){
    for($i = $num; $i>=1;$i--){
        if($i>1){
            $aux = $a[$i-1];
            $a[$i-1] = $a[$i];
            print_r($a);
        }
    }
}
    
asked by Borja Sanchez 06.07.2017 в 12:00
source

2 answers

1

Even though the partner solution works, I think it would be much more optimal and simple to do it in the following way without using nested loops that are always a bad idea.

<?php 
$a = Array("a","b","c","d");

// extraemos el elemento
$corte = array_splice ( $a, 1, 1 );

// añadimos el elemento al final
array_push ( $a, $corte[0] );

print_r ( $a );
// Array ( [0] => a [1] => c [2] => d [3] => b )
    
answered by 06.07.2017 / 16:38
source
1

Reordering your code a bit so it should be worth it.

$a = Array("a","b","c","d");
print_r($a);
$num = count($a);

for($i=1;$i<$num-1;$i++){
        $aux = $a[1];
        for($j=1;$j<$num-1;$j++){
            $a[$j] = $a[$j+1];
        }
        $a[$num-1] = $aux;
        print_r($a);
}

Exit:

Array                                                                                                                                                                                                                             
(                                                                                                                                                                                                                                 
    [0] => a                                                                                                                                                                                                                      
    [1] => b                                                                                                                                                                                                                      
    [2] => c                                                                                                                                                                                                                      
    [3] => d                                                                                                                                                                                                                      
)                                                                                                                                                                                                                                 
Array                                                                                                                                                                                                                             
(                                                                                                                                                                                                                                 
    [0] => a                                                                                                                                                                                                                      
    [1] => c                                                                                                                                                                                                                      
    [2] => d                                                                                                                                                                                                                      
    [3] => b                                                                                                                                                                                                                      
)                                                                                                                                                                                                                                 
Array                                                                                                                                                                                                                             
(                                                                                                                                                                                                                                 
    [0] => a                                                                                                                                                                                                                      
    [1] => d                                                                                                                                                                                                                      
    [2] => b                                                                                                                                                                                                                      
    [3] => c                                                                                                                                                                                                                      
)       
    
answered by 06.07.2017 в 14:02