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.