Q:

Write a PHP program to create a new array using the first n strings from a given array of strings. (n>=1 and <=length of the array)

0

Write a PHP program to create a new array using the first n strings from a given array of strings. (n>=1 and <=length of the array).

All Answers

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

<?php
function test($arr_str, $n)
    { 
        $new_array = [sizeof($arr_str)];

            for ($i = 0; $i < $n; $i++)
            {
                $new_array[$i] = $arr_str[$i];
            }

            return $new_array;
    }
    

$result = test(["a", "b", "bb", "c", "ccc"], 3);
echo implode(" ",$result);

Sample Output:

a b bb

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