Write a PHP program to check whether the sequence of numbers 1, 2, 3 appears in a given array of integers somewhere
<?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)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer