Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:41:01+00:00 2026-06-15T15:41:01+00:00

I have two associative arrays that can look as follows: $prod1 = ( [name]

  • 0

I have two associative arrays that can look as follows:

$prod1 = (
    [name] = 'John',
    [address] = 'Milky way',
    [city] = 'Miami',
);

$prod2 = (
    [address] = 'Not so milky way'
);

$prod1 has always got the same kind of elements (name, address, city), but $prod2 could contain of either all of the elements or some of them

What I want is to replace the value in the elements in $prod1 that matches with the elements in $prod2. In the example above $prod2 contains of address, and therefore the address in $prod1 should be replaced with the address in $prod2 (the rest of the elements should of course stay as is).

How can this be accomplished?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:41:03+00:00Added an answer on June 15, 2026 at 3:41 pm

    The array_merge function will do this for you:

    $prod_new = array_merge($prod1, $prod2);
    

    From the documentation:

    If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.

    Alternatively, you can use the + operator, which will give you the same result:

    $prod_new = $prod1 + $prod2;
    

    The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have two associative arrays, what would be the most efficient way of
I have two associative arrays in PHP that are defined as following: $this_week[] =
I have two arrays that looks like this: Array 1, $ids: $ids = array('8',
I have a two dimensional associative array which contains a map of blocks that
I have two associative arrays: ArrayA = array( [10] => ten [12] => twelve
I have two arrays, both associative; ArrayUneaten ( lemon=> 7, banana=>6, apple=>10)//the units of
So I have a function that merges two arrays replacing variables that are in
How can I merge this three arrays $name ={Tom, John, David}; $v1 = {Tom:100,
I have two models User and Category that have a HABTM association. I would
I'm trying to calculate the dot-product of two very sparse associative arrays. The arrays

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.