[Solved] php array merge problem

Source: Internet
Author: User
Problem encountered: There are two arrays. it is known that the IDs in array 1 are equal to the pid in array 2. the problem is, merge img_url in array 2 to the corresponding id in array 1. The final implementation is required: Array ([0] & amp; gt; Array ([id] & amp; gt; 7 [collectio...

Problems:
Currently, there are two arrays. it is known that the IDs in array 1 are equal to the pid in array 2. the problem is, merge img_url in array 2 to the corresponding id in array 1.

The final implementation is required:

Array ([0] => Array ([id] => 7 [collection_id] => 1 [prize_num] => 1 [prize_name] => Minus 20 RMB [total] => 10, ** [url_img] =>/upload/business/1476342419.png **) [1] => Array ([id] => 8 [collection_id] => 1 [prize_num] => 2 [prize_name] => Off [total] => 20, ** [url_img] =>/upload/business/1476348963.jpg **)

Problem solved

First, run the first loop, and then run the second loop in it. go to the second array to find the qualified item foreach ($ shopPrizeName as $ key => $ value) {foreach ($ shopPImagesName as $ k => $ v) {if ($ value ['id'] = $ v ['pid']) {$ value ['IMG _ url'] = $ v ['IMG _ url'] ;}}$ shopData [] = $ value ;}print_r ($ shopData );

Reply content:

Problems:
Currently, there are two arrays. it is known that the IDs in array 1 are equal to the pid in array 2. the problem is, merge img_url in array 2 to the corresponding id in array 1.

The final implementation is required:

Array ([0] => Array ([id] => 7 [collection_id] => 1 [prize_num] => 1 [prize_name] => Minus 20 RMB [total] => 10, ** [url_img] =>/upload/business/1476342419.png **) [1] => Array ([id] => 8 [collection_id] => 1 [prize_num] => 2 [prize_name] => Off [total] => 20, ** [url_img] =>/upload/business/1476348963.jpg **)

Problem solved

First, run the first loop, and then run the second loop in it. go to the second array to find the qualified item foreach ($ shopPrizeName as $ key => $ value) {foreach ($ shopPImagesName as $ k => $ v) {if ($ value ['id'] = $ v ['pid']) {$ value ['IMG _ url'] = $ v ['IMG _ url'] ;}}$ shopData [] = $ value ;}print_r ($ shopData );

You have solved the problem on your own. here is another method.

/*** Extract a column 'IMG _ url' from the multi-dimensional array to form a new array, and use the id in the multi-dimensional array as the key * Of course, you can also use the array_column to splice the array */$ idImgMap = array_column ($ shopImageName, 'IMG _ url ', 'id'); foreach ($ shopPrizeName as & $ value) {$ value ['IMG _ url'] = $ idImgMap [$ value ['id'];}

The algorithm complexity of this implementation is 2O (n), and your algorithm is O (n ^ 2), so this performance will be better.

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.