Q:

Write a PHP program to shift an element in left direction and return a new array

0

Write a PHP program to shift an element in left direction and return a new array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function test($numbers)
    { 
      $size = sizeof($numbers);
      $shiftNums = [$size];

      for ($i = 0; $i < $size; $i++)
           {
               $shiftNums[$i] = $numbers[($i + 1) % $size];
           }
      return $shiftNums;
      }   

$result = test([10, 20, -30, -40, 50] );
echo "New array: " . implode(',', $result);

Sample Output:

New array: 20,-30,-40,50,10

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now