Menu
Coddy logo textTech

Merging Associative Arrays

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

While array_merge() works seamlessly with indexed arrays, its behavior changes significantly when working with associative arrays. The key difference is how it handles duplicate string keys between the arrays being merged.

When merging associative arrays, if both arrays contain the same string key, the value from the later array will overwrite the value from the earlier array:

<?php
$defaults = ["theme" => "light", "language" => "english", "notifications" => true];
$userSettings = ["theme" => "dark", "notifications" => false];
$finalConfig = array_merge($defaults, $userSettings);
print_r($finalConfig);
// Outputs: Array ( [theme] => dark [language] => english [notifications] => false )
?>

Notice how "theme" changed from "light" to "dark" and "notifications" changed from true to false, while "language" remained "english" since it wasn't overridden. This behavior makes array_merge() perfect for configuration systems where you want default values to be overridden by user preferences.

The order of arguments matters—values from arrays listed later in the function call take precedence over earlier ones. This overwriting behavior only applies to string keys; numeric keys are always re-indexed sequentially, just like with indexed arrays.

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 two inputs: a default configuration array and a user preferences array, both in JSON format. Read both inputs, convert each JSON string to an associative array, use array_merge() to combine them (with user preferences overriding defaults), and print the merged configuration using print_r().

Input format: Two lines - each line contains a JSON object representing an associative array (example: {"theme":"light","language":"english"})

Expected output: The merged configuration array where user preferences override default values, displayed using print_r()

Cheat sheet

When merging associative arrays with array_merge(), duplicate string keys are overwritten by values from later arrays:

<?php
$defaults = ["theme" => "light", "language" => "english", "notifications" => true];
$userSettings = ["theme" => "dark", "notifications" => false];
$finalConfig = array_merge($defaults, $userSettings);
print_r($finalConfig);
// Outputs: Array ( [theme] => dark [language] => english [notifications] => false )
?>

Key behaviors:

  • String keys with same name: later array values override earlier ones
  • Order matters: arrays listed later take precedence
  • Numeric keys are always re-indexed sequentially
  • Perfect for configuration systems with default values and user overrides

Try it yourself

<?php
// Read the two JSON inputs
$defaultConfig = fgets(STDIN);
$userPreferences = fgets(STDIN);

// Convert JSON strings to associative arrays
$defaultArray = (array)json_decode($defaultConfig, true);
$userArray = (array)json_decode($userPreferences, true);

// TODO: Write your code below to merge the arrays


// Output the merged configuration
print_r($mergedConfig);
?>
quiz iconTest yourself

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

All lessons in Logic & Flow