
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Merge two arrays keeping original keys in PHP
To merge two arrays keeping original keys in PHP, the code is as follows−
Example
<?php $arr1 = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110"); $arr2 = array("t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" ); var_dump ($arr1 + $arr2); ?>
Output
This will produce the following output−
array(8) { ["p"]=> string(3) "150" ["q"]=> string(3) "100" ["r"]=> string(3) "120" ["s"]=> string(3) "110" ["t"]=> string(3) "115" ["u"]=> string(3) "103" ["v"]=> string(3) "105" ["w"]=> string(3) "125" }
Example
Let us now see another example −
<?php $arr1 = array(); $arr2 = array("a" => "Jacob"); var_dump ($arr1 + $arr2); ?>
Output
This will produce the following output−
array(1) { ["a"]=> string(5) "Jacob" }
- Related Articles
- Merge two sorted arrays in Java
- Merge two sorted arrays in C#
- How to merge two arrays in JavaScript?
- Merge two sorted arrays using C++.
- Merge two sorted arrays in Python using heapq?
- Merge two arrays with alternating Values in JavaScript
- Merge two arrays using C# AddRange() method
- How can we merge two JSON arrays in Java?
- Maximum array from two given arrays keeping order same in C++
- JavaScript - Merge two arrays according to id property
- How to merge two arrays without duplication in android listview?
- Merge two sorted arrays into a list using C#
- C# program to merge two sorted arrays into one
- How to merge two arrays with objects in one in JavaScript?
- Merge two sorted arrays to form a resultant sorted array in JavaScript

Advertisements