Q:

Write a PHP program to create a new array of given length using the odd numbers from a given array of positive integers

0

Write a PHP program to create a new array of given length using the odd numbers from a given array of positive integers.

All Answers

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

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

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