w3resource

PHP Exercises: Create a new array containing the middle elements from the two given arrays of integers, each length 5


93. New Array from Middle Elements of Two Arrays

Write a PHP program to create a new array containing the middle elements from the two given arrays of integers, each length 5.

Sample Solution:

PHP Code :

<?php
// Define a function named 'test' that takes two arrays as parameters
function test($nums1, $nums2)
 { 
    // Return a new array containing the third element from both input arrays
    return [$nums1[2], $nums2[2]];
 }   

// Create two arrays $nums1 and $nums2 with numeric values
$nums1 = [10, 20, -30, -40, 30];
$nums2 = [10, 20, 30, 40, 30];

// Print the original contents of array $nums1 as a string using 'implode' and 'echo'
echo "Original array1: " . implode(',', $nums1) . "\n";

// Print the original contents of array $nums2 as a string using 'implode' and 'echo'
echo "Original array2: " . implode(',', $nums1) . "\n";

// Call the 'test' function with arrays $nums1 and $nums2 and store the result in $result
$result = test($nums1, $nums2);

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

Explanation:

  • Function Definition:
    • A function named test is defined, which takes two parameters:
      • $nums1: the first input array.
      • $nums2: the second input array.
  • Returning Elements:
    • The function returns a new array that contains:
      • The third element from $nums1 ($nums1[2]).
      • The third element from $nums2 ($nums2[2]).
  • Array Initialization:
    • Two arrays are created:
      • $nums1 = [10, 20, -30, -40, 30]
      • $nums2 = [10, 20, 30, 40, 30]
  • Display Original Arrays:
    • The contents of $nums1 are printed using implode to format it as a string:
    • The contents of $nums2 are intended to be printed, but there is a mistake in the code. It incorrectly prints $nums1 again instead of $nums2.
  • Function Call:
    • The test function is called with the two arrays, and the result is stored in the variable $result.
  • Display New Array:
    • The new array, which contains the third elements from both input arrays, is printed using implode:

Output:

Original array1: 10,20,-30,-40,30
Original array2: 10,20,-30,-40,30
New array: -30,30

Flowchart:

Flowchart: Create a new array containing the middle elements from the two given arrays of integers, each length 5.

For more Practice: Solve these Related Problems:

  • Write a PHP script to extract the middle element from each of two five-element arrays and merge them into a new array.
  • Write a PHP function to retrieve the central elements from two arrays and return a new two-element array.
  • Write a PHP program to identify and concatenate the middle values of two given arrays into a resultant array.
  • Write a PHP script to use indexing to access the median elements of two arrays and create a new merged array.

Go to:


PREV : Replace All Elements with Maximum of Boundaries.
NEXT : New Array of First and Last Elements.

PHP Code Editor:



Contribute your code and comments through Disqus.

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.