Write a PHP program to create a new array of given length using the odd numbers from a given array of positive integers.
<?php function test($nums, $count) { $evens = [$count]; $j = 0; for ($i = 0; $j < $count; $i++) { if ($nums[$i] % 2 != 0) { $evens[$j] = $nums[$i]; $j++; } } return $evens; } $result = test([1,2,3,5,7,9,10], 3); echo "New array: ".implode(",", $result);
Sample Output:
New array: 1,3,5
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