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

  • برمجة بي اتش بي

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.

الأجوبة

<?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
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...