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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou 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
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting