Q:

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

0

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

All Answers

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

<?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

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