PHP Program to right shift elements in an array


April 28, 2022, Learn eTutorial
1241

What is meant by a right shift in an array?

In this program, we are going to right-shift the elements in the array number of times. To perform the right shift on elements first we have to assign a value to the variable n to shift the index of the elements of array times.

For example, if the array is 5, 9, 8, 6, 7, 3, 4, 2, 1 and n is 3 then the output will be 4, 2, 1, 5, 9, 8, 6, 7, 3

Right shift elements in an array

How to right shift elements in an array using PHP?

For this program to right shift elements in the array, we are taking the static values which are assigned to the array a[ ]. Then we have to assign the value of how many times right-shift has to perform on the array into the variable n, and assign the size of the array into the variable len using the built-in function count(). After that, we have to assign the value 0 into the variable i and perform the for loop until the condition 'i < n' and also increment the value of the variable i in every iteration and in the loop block we have to assign the element of a[len - 1] into the variable temp and then we have to open another for loop and assign the value 'len - 1' into the variable j, perform the loop until the condition 'j > 0', also decrement the value of the variable j in every iteration and in the loop block we have to shift the value of a[j - 1]  into a[j]. After the completion of 2nd loop, we have to assign the value of the variable temp to a[0]. After the completion of the main loop, we can print the array a[ ] as the right-shifted array.

ALGORITHM

Step 1: Initialize an array a[ ] with values

Step 3: Assign the number of times the array should perform the right shift into variable n

Step 4: Assing the size of the array into variable len using the built-in function count()

Step 5: Print the element in the array a[ ]

Step 6: To right shift the elements of array n times first assign the value 0 into the variable i and perform the sub-steps until the condition 'i < n' becomes false and increment the value of variable i in every iteration

  1. Assign the element of a[len - 1] to the variable temp
  2. Assign the value 'len - 1' into the variable j and, assign the value of 'a[j - 1] into variable a[j] until the condition 'j > 0' becomes false and decrement the value of variable in every iteration
  3. Assign the value of temp to the array a[0]

Step 7: After the completion of step 6 print the elements of the array a[ ] as n times right-shifted 

PHP Source Code

                                          <?php
$a = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$n = 4;
$len = count($a);
echo "Elements in the array: \n";
for ($i = 0; $i < $len; $i++) {
    echo "$a[$i] ";
}
for ($i = 0; $i < $n; $i++) {
    $temp = $a[$len - 1];
    for ($j = $len - 1; $j > 0; $j--) {
        $a[$j] = $a[$j - 1];
    }
    $a[0] = $temp;
}
echo "\nArray after the right shift of $n times:\n";
for ($i = 0; $i < $len; $i++) {
    echo "$a[$i] ";
}
                                      

OUTPUT

Elements in the array:
1 2 3 4 5 6 7 8 9
Array after the right shift of 4 times:
6 7 8 9 1 2 3 4 5