Menu
Coddy logo textTech

Merging Indexed Arrays

Part of the Logic & Flow section of Coddy's PHP journey — lesson 14 of 68.

When you need to combine multiple indexed arrays into one larger array, PHP's array_merge() function provides the perfect solution. This function takes two or more arrays as arguments and creates a new array containing all elements from the input arrays.

Here's the basic syntax:

<?php
$firstList = ["Alice", "Bob"];
$secondList = ["Charlie", "Diana"];
$combined = array_merge($firstList, $secondList);
print_r($combined);  // Outputs: Array ( [0] => Alice [1] => Bob [2] => Charlie [3] => Diana )
?>

Notice how array_merge() creates a completely new array rather than modifying the original arrays. The elements from the first array appear first, followed by elements from the second array, and so on. The new array is automatically re-indexed starting from 0.

You can merge more than two arrays at once:

<?php
$morning = ["John", "Sarah"];
$afternoon = ["Mike", "Lisa"];
$evening = ["Tom"];
$allGuests = array_merge($morning, $afternoon, $evening);
print_r($allGuests);  // Outputs: Array ( [0] => John [1] => Sarah [2] => Mike [3] => Lisa [4] => Tom )
?>

This function is particularly useful when you're collecting data from different sources and need to combine them into a single list, such as merging guest lists from different events or combining product catalogs from multiple categories.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You will receive three inputs: two arrays in JSON format and a third array also in JSON format. Read all three inputs, convert each JSON string to an array, use array_merge() to combine all three arrays into one, and print the merged array using print_r().

Input format: Three lines - each line contains a JSON array (example: ["apple","banana"])

Expected output: The merged array containing all elements from the three arrays in order, displayed using print_r()

Cheat sheet

Use array_merge() to combine multiple indexed arrays into one larger array:

<?php
$firstList = ["Alice", "Bob"];
$secondList = ["Charlie", "Diana"];
$combined = array_merge($firstList, $secondList);
print_r($combined);  // Outputs: Array ( [0] => Alice [1] => Bob [2] => Charlie [3] => Diana )
?>

array_merge() creates a new array without modifying the original arrays. Elements are combined in order and automatically re-indexed starting from 0.

You can merge more than two arrays at once:

<?php
$morning = ["John", "Sarah"];
$afternoon = ["Mike", "Lisa"];
$evening = ["Tom"];
$allGuests = array_merge($morning, $afternoon, $evening);
print_r($allGuests);  // Outputs: Array ( [0] => John [1] => Sarah [2] => Mike [3] => Lisa [4] => Tom )
?>

Try it yourself

<?php
// Read the three JSON inputs
$input1 = trim(fgets(STDIN));
$input2 = trim(fgets(STDIN));
$input3 = trim(fgets(STDIN));

// Convert JSON strings to arrays
$array1 = (array)json_decode($input1, true);
$array2 = (array)json_decode($input2, true);
$array3 = (array)json_decode($input3, true);

// TODO: Write your code below to merge the arrays and print the result

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow