w3resource

PHP Exercises: Create a new array from two give array of integers, each length 3

PHP Basic Algorithm: Exercise-101 with Solution

Write a PHP program to create a new array from two give array of integers, each length 3.

Sample Solution:

PHP Code :

<?php
// Define a function named 'test' that takes two arrays as parameters
function test($nums1, $nums2)
 { 
    // Return a new array combining the elements of $nums1 and $nums2
    return [$nums1[0], $nums1[1], $nums1[2], $nums2[0], $nums2[1], $nums2[2]];
 }   

// Call the 'test' function with arrays [10, 20, 30] and [40, 50, 60] and store the result in $result
$result = test([10, 20, 30], [40, 50, 60]);

// Print the new array containing elements from both input arrays using 'implode' and 'echo'
echo "New array: " . implode(',', $result);
?>

Sample Output:

New array: 10,20,30,40,50,60

Flowchart:

Flowchart: Create a new array from two give array of integers, each length 3.

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program to create an array taking two middle elements from a given array of integers of length even.
Next: Write a PHP program to create a new array swapping the first and last elements of a given array of integers and length will be least 1.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.