PHP Program to left shift elements in an array


April 19, 2022, Learn eTutorial
1158

What is meant by a left shift in an array?

In this program, we are going to left-shift the elements in the array number of times. To perform the left 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 2 then the output will be 8 6 7 3 4 2 1 5 9.

How to left shift elements in an array using PHP?

For this program to left 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 left 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[0] into the variable temp then we have to assign the value 0 into the variable j and perform the loop until the condition 'j < len - 1' and also increment 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[j]. After the completion of the main loop, we can print the array a[] as the left-shifted array.

ALGORITHM

Step 1: Initialize an array a[] with values

Step 3: Assign the number of times the array should perform the left 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 left 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[0] to the variable temp

        (ii) Assign the value 0 into the variable j and perform the sub-step until the condition 'j < len - 1' becomes false and increment the value of variable j               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[j]

Step 7: After the completion of step 6 print the elements of the array a[] as left-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[0];
    for ($j = 0; $j < $len - 1; $j++) {
        $a[$j] = $a[$j + 1];
    }
    $a[$j] = $temp;
}
echo "\nArray after the left 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 left shift of 4 times:
5 6 7 8 9 1 2 3 4