 # PHP Program to right shift elements in an array

## 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  and shift the index of the element times. For example, 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.

## 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 the number of times the element hast perform right-shift into the variable and assign the size of the array into the variable len using the built-in function `count() `and after that, we have to assign the value 0 into the variable i and perform the 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 then we have to assign the value 'len - 1' into the variable j and perform the loop until the condition 'j > 0' and also decrement the value of the variable j in every iteration and in the loop block we have to assign the value of a[j - 1]  into the array a[j] after the completion of this loop we have to assign the value of the variable temp to the array a. 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 the variable n

Step 4: Assing the size of an array into the 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

(i) Assign the element of a[len - 1] to the variable temp

(ii) Assign the value 'len - 1' into the variable j and perform the sub-step until the condition 'j > 0' becomes false and decrement the value of                             variable in every iteration

(a) Assign the value of 'a[j - 1] into variable a[j]

(iii) Assign the value of temp to the array a

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

## 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 = \$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```
VIEW ALL
VIEW ALL
##### OtherTutorials
VIEW ALL 