How to solve array merging problems

Source: Internet
Author: User
For array merging, please help me to have two arrays, for example, arr1 = array (& nbsp; & quot; name & quot ;=& gt; Wang xiao, & nbsp; & quot; qty & quot; = & gt; 2) arr2 = array (& nbsp; & quot; Qty & quot; = & gt; 4, & nbs
I have two arrays, for example:
Arr1 = array (
"Name" => Wang xiao,
"Qty" => 2
)

Arr2 = array (
"Qty" => 4,
"Memo" => description
)

The above two arrays are merged to achieve the following results:
Arr3 = array (
"Name" => Wang xiao,
"Qty" => 6,
"Memo" => description
)
Add Qty. Does PHP have direct function implementation? I cannot use array_merge, but I will replace the original Qty.

------ Solution --------------------
$ Arr = array_merge_recursive ($ arr1, $ arr2 );

$ Arr: Array
(
[Name] => Wang Xiaoyu
[Qty] => Array
(
[0] => 2
[1] => 4
)

[Memo] => description
)

But this is not what you want, so you have to write a function by yourself.
------ Solution --------------------
You can only write functions by yourself.
------ Solution --------------------
$ Arr = array_merge_recursive ($ arr1, $ arr2 );
$ Arr ['qty '] = array_sum ($ arr ['qty']);
------ Solution --------------------
No direct function implementation

However, if you use foreach, just a few lines of work, just write the function. What else do you need?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.