Using PHP array_merge() to Combine Indexed and Associative Arrays

This article explains PHP's array_merge() function, detailing its syntax, demonstrating how to merge indexed and associative arrays with multiple examples, and highlighting how overlapping keys are handled, providing developers with practical guidance for efficient array manipulation.

php Courses
php Courses
php Courses
Using PHP array_merge() to Combine Indexed and Associative Arrays

PHP provides a powerful function array_merge() that can combine one or more arrays into a new array and return the result.

The syntax of array_merge() is straightforward:

array_merge ( array $array1 [, array $... ] ) : array

The function accepts multiple arrays as parameters and returns a single merged array containing all elements.

Example 1: Merging Two Indexed Arrays

$array1 = array('apple', 'banana', 'orange');
$array2 = array('kiwi', 'melon', 'grape');
$result = array_merge($array1, $array2);
print_r($result);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => kiwi
    [4] => melon
    [5] => grape
)

This merges the two arrays so that the new array contains all elements from both original arrays.

Example 2: Merging Multiple Indexed Arrays

$array1 = array('apple', 'banana', 'orange');
$array2 = array('kiwi', 'melon', 'grape');
$array3 = array('strawberry', 'pineapple');
$result = array_merge($array1, $array2, $array3);
print_r($result);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => kiwi
    [4] => melon
    [5] => grape
    [6] => strawberry
    [7] => pineapple
)

The function can accept any number of arrays, concatenating them in the order provided.

Example 3: Merging Associative Arrays

$array1 = array('name' => 'John', 'age' => 25);
$array2 = array('name' => 'Jane', 'email' => '[email protected]');
$result = array_merge($array1, $array2);
print_r($result);

Output:

Array
(
    [name] => Jane
    [age] => 25
    [email] => [email protected]
)

When merging associative arrays, duplicate keys are overwritten by later arrays, so the value from the last array is retained.

Conclusion

The array_merge() function is a versatile tool for combining both indexed and associative arrays in PHP, offering a concise and efficient way to handle array data.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendmergeArrayphp-functionsarray merge
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.