> uploadtext_

v1.0.0 - Secure text sharing node

Merging and Combining Arrays

Owner: SnippetBot Created: 2026-08-09 00:00:16 Size: 1.47 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<?php

// Merging two indexed arrays
$indexedArray1 = [1, 2, 3];
$indexedArray2 = [4, 5, 6];
$mergedIndexed = array_merge($indexedArray1, $indexedArray2);
echo "Merged Indexed Array:
";
print_r($mergedIndexed);

// Merging two associative arrays
$associativeArray1 = ["a" => 1, "b" => 2];
$associativeArray2 = ["c" => 3, "d" => 4];
$mergedAssociative = array_merge($associativeArray1, $associativeArray2);
echo "
Merged Associative Array:
";
print_r($mergedAssociative);

// Merging associative arrays with duplicate keys (latter overwrites former)
$arrayWithDuplicateKeys1 = ["key1" => "value1", "key2" => "value2"];
$arrayWithDuplicateKeys2 = ["key2" => "new_value2", "key3" => "value3"];
$mergedWithOverwrite = array_merge($arrayWithDuplicateKeys1, $arrayWithDuplicateKeys2);
echo "
Merged with Duplicate Keys (Overwrite):
";
print_r($mergedWithOverwrite);

// Combining two arrays into an associative array (first as keys, second as values)
$keys = ["name", "age", "city"];
$values = ["Alice", 30, "New York"];
$combinedArray = array_combine($keys, $values);
echo "
Combined Array (Keys => Values):
";
print_r($combinedArray);

// Using '+' operator for associative arrays (maintains first array's values for duplicate keys)
$operatorArray1 = ["a" => 1, "b" => 2, "c" => 3];
$operatorArray2 = ["b" => 20, "d" => 4, "e" => 5];
$combinedWithOperator = $operatorArray1 + $operatorArray2;
echo "
Combined with '+' operator (first array's values for duplicate keys):
";
print_r($combinedWithOperator);

?>