Write a PHP program to check a given array of integers, length 3 and create a new array. If there is a 5 in the given array immediately followed by a 7 then set 7 to 1

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

Write a PHP program to check a given array of integers, length 3 and create a new array. If there is a 5 in the given array immediately followed by a 7 then set 7 to 1

الأجوبة

<?php
function test($numbers)
 { 
    for ($i = 0; $i < sizeof($numbers) - 1; $i++)
            {
                if ($numbers[$i] == 5 && $numbers[$i + 1] == 7)
                    $numbers[$i + 1] = 1;
            }
            return $numbers;
    
 }   

$a = [1, 5, 7];
echo "Original array: " . implode(',', $a) . "\n";
$result = test($a);
echo "New array with maximum values: " . implode(',', $result);

Sample Output:

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

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

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