Q:

Write a PHP program to check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere

0

Write a PHP program to check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere

All Answers

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

<?php
function test($nums)
{
     for ($i = 0; $i < sizeof($nums)-2; $i++)
            {
                if ($nums[$i] == 1 && $nums[$i + 1] == 2 && $nums[$i + 2] == 3)
                    return true;
            }
           return false;
 }

var_dump(test(array(1,1,2,3,1)));
var_dump(test(array(1,1,2,4,1)));
var_dump(test(array(1,1,2,1,2,3)));

Sample Output:

bool(true)
bool(false)
bool(true)

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