Q:

Write a PHP program to create a new array swapping the first and last elements of a given array of integers and length will be least 1

0

Write a PHP program to create a new array swapping the first and last elements of a given array of integers and length will be least 1.

All Answers

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

<?php
function test($numbers)
 { 

     $first = $numbers[0];
     $numbers[0] = $numbers[sizeof($numbers) - 1];
     $numbers[sizeof($numbers) - 1] = $first;
     return $numbers;
 }   

$result = test([1, 5, 7, 9, 11, 13] );
echo "New array, after swapping first and last elements: " . implode(',', $result);

Sample Output:

New array, after swapping first and last elements: 13,5,7,9,11,1

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