Skip to content

DataOps Blog

Just another DataOps site

  • Home
  • certification
  • consultant
  • consulting
  • Contact Us!
  • courses
  • Intro to Laravel
  • tools
  • trainer
  • training

PHP | array_merge() and array_merge_recursive() functions

January 3, 2022
By Manish Kumar In PHP

PHP | array_merge() and array_merge_recursive() functions

PHP array_merge()

This array function is used to merge or combine multiple arrays into one new single array. In simple if their are two arrays as array1 and array2 and none of the elements of this two arrays have the same key then using this array_merge function in PHP, we can easily combine both the arrays and it will be displayed as newArray1. and also you can declare this new array in any variable and use it further.


Example: So in the following code we have declared two different arrays with different keys and we have combined them using the array_merge() .

Output :-

Array
(
    [0] => digital
    [1] => Technical
    [2] => Bullshop
    [3] => Levies
    [4] => london
    [5] => brazil
    [6] => aakash
)

PHP array_merge_recursive()

This array function in PHP is a type of function that is used to combine or merge multiple arrays into one single array. In simple, if their are two arrays1 as array2 and array2 and atleast two elements of this two arrays have the same key or values then using this method of array_merge-recursive() function in PHP, we can combine this both the arrays and it will be displayed as newArray2. If you assign only one array to this function then it will act same as that of array_merge().

Example: So in the following code we have declared two arrays in with two of the elements has the same keys and using the array_merge_recursive() we have combined them successfully.

Output :-

Output :-

Array
(
    [a] => Array
        (
            [0] => digital
            [1] => london
        )

    [b] => Array
        (
            [0] => Technical
            [color] => Array
                (
                    [0] => green
                    [1] => red
                    [2] => yellow
                )

        )

    [c] => Bullshop
    [d] => Levies
    [0] => aakash
)

Array
(
    [a] => london
    [b] => Array
        (
            [color] => Array
                (
                    [0] => green
                    [1] => red
                    [2] => yellow
                )

        )

    [c] => Bullshop
    [d] => Levies
    [0] => aakash
)

Difference between array_merge() and array_merge_recursive()

array_merge()

  • This function is used when elements of array has different keys
  • Syntax: array_merge($array1, $array2, $array3…..and more);

array_merge_recursive()

  • This function is used when elements of array has same keys
  • Syntax: array_merge_recursive($array1, $array2, $array3….. and more);

I hope you like this information about array function in php. Lot more to come.

Thank You !!

composer css html javascript jquery Laravel laravel-form MySQL php xampp
Written by:

Manish Kumar

View All Posts

Recent Posts

  • The Online Dating Association (ODA) Maintains the Gold Standard for online dating sites in britain
  • Tips Balance On The Internet And Typical Dating
  • meet black gay men Gay Hookups
  • (no title)
  • (no title)
  • Online Casinos Offer No Deposit Casino Bonus Codes
  • A Board Website Review
  • How you can Remove Disease From ipad device
  • Dating – When You Should Discuss Dirty Little Methods?
  • How to Find the Top Canadian Online Casino
  • Research Papers For Sale
  • Free Slot Machine Games – Tips to Play Slots Free
  • (no title)
  • Why Use a VPN Online?
  • The very best GTA Games
  • DesignLab Xbox 360 – Create Your Very Own Controllers
  • Tips on how to Conduct Individual Businesses
  • Tips on how to Download the Best Android Programs
  • Finest Antivirus Totally free
  • VPN USA — Protect Your web Privacy and Unblock Geo-Restricted Content

Archives

Categories

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
  • Free Video Tutorials
  • DevOps School
  • Best DevOps
  • scmGalaxy
  • Artificial Intelligence
  • DataOps
  • AIOps
  • GuruKul Galaxy
  • DevOps Consulting
  • DevOps Freelancers
  • DevOps Trainer
  • Free Ebooks
  • School for Debugger
  • Holiday Landmark
  • Surgery Planet
  • My Hospital Now
  • My Medic Plus
  • ProfessNow
  • Cotocus
  • Stocks Mantra
  • I Reviewed

Proudly powered by WordPress | Theme: BusiCare by SpiceThemes