Write a PHP program to create a new array taking the first and last elements of a given array of integers and length 1 or more

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

Write a PHP program to create a new array taking the first and last elements of a given array of integers and length 1 or more.

 

الأجوبة

<?php
function test($nums)
 { 
      return [$nums[0], $nums[sizeof($nums) - 1]];
    
 }   

$a = [10, 20, -30, -40, 30];

echo "Original array: " . implode(',', $a) . "\n";

$result = test($a);

echo "New array: " . implode(',', $result);

Sample Output:

Original array: 10,20,-30,-40,30
New array: 10,30
هل كان المحتوى مفيد؟

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

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