3

Working with PHP. I have array 1 with this keys and values:

$array_1 = array(
(more values)
'propub_cost_max' => 5,
'propub_cost_min' => 0.5,
'average_calc_last' => '-1 Months',
'propub_qtd_first_offer' => 4
);

and array 2:

$array_2 = array(
'propub_cost_max' => 20,
'propub_cost_min' => (no value),
'average_calc_last' => (no value),
'propub_qtd_first_offer' => (no value)
);

I want to merge array 2 with array 1 so i did:

$result = array_merge($array_2, $array_1);

But the result is:

$result = array(
(more values)
'propub_cost_max' => 5,
'propub_cost_min' => 0.5,
'average_calc_last' => '-1 Months',
'propub_qtd_first_offer' => 4
);

The propub_cost_max key should assume the value 20, right?

The idea is to maintain some values, and replace where the values are different if there is any value, of course. I thought that array_merge should work, but...

Thank you all.

3
  • 2
    You have a } instead of a parenthesis. Commented Nov 6, 2016 at 20:17
  • 3
    "If the input arrays have the same string keys, then the later value for that key will overwrite the previous one." php.net/manual/en/function.array-merge.php. in your case array_1 is the latter Commented Nov 6, 2016 at 20:17
  • Thank you @TheValyreanGroup Commented Nov 6, 2016 at 20:52

2 Answers 2

3

"If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.". in your case array_1 is the latter.

Says nogad.

(link to array_merge )

And also

You have a } instead of a parenthesis.

As says TheValyreanGroup.

Both of these are absolutely correct. So

$result = array_merge($array_1, $array_2); 

Will solve your problem. Values are swapped so that $array_2 will now overwrite the values in $array_1.

To solve your overall issue of wanting to update some values, without knowing which values and which conditions you want to preserve, we're reduced to simply not overwriting values with empty ones, so :

$array_2 = array_filter($array_2); //clears empty values
$result = array_merge($array_1, $array_2); // as before. updates non-empty new values.
Sign up to request clarification or add additional context in comments.

7 Comments

The } is obviously a copying error, otherwise the script wouldn't even run and he wouldn't get any result at all.
Well it will solve the problem in this test case, but the desired behavior in the general case is presumably that max gets the larger of the 2 values, not the second of the two (even though swapping them here helps).
@Barmar well yes, but I think it's still worth pointing that comment out as typos are often critical in causing errors and a user with a habit of typos (and of not spotting and correcting them) will have lotsof code issues they can't solve themselves.
seeing as how I generate an answer from other peoples comment I've communitied it, so feel free to edit as you need, @JeremyKahan
@Martin that is very sporting of you, but I do not know PHP well enough to code it element-wise so if the key contains min one chooses the smaller of the 2 values (assuming both exist), if it contains max, choose the larger, and otherwise punt.
|
1

This will solve your problem

array_merge_recursive($array_1, $array_2);

3 Comments

Good job. So why does this work? What is the difference ?
array_merge replaces the new old value with the new value if there is a match between key names. but array_merge_recursive puts them inside one big array together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.