Write a PHP program to create a new array taking the first two elements from a given array

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

Write a PHP program to create a new array taking the first two elements from a given array. If the length of the given array is less than 2 then return the give array

الأجوبة

<?php
function test($numbers)
 { 

      if (sizeof($numbers) >= 2)
        {
            $front_nums = [$numbers[0], $numbers[1]];
        }
      else if (sizeof($numbers) > 0)
        {
            $front_nums = [$numbers[0]];
        }
      else
        {
            $front_nums = [];
        }
 
     return $front_nums;
 }   

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

Sample Output:

New array: 1,5
هل كان المحتوى مفيد؟

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

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