[Solved] PHP array merging problem

WBOY
Release: 2023-03-03 11:10:01
Original
1073 people have browsed it

Problems encountered:
Currently there are two arrays. It is known that in the two arrays, the id in array 1 is equal to the pid in array 2. The problem is to merge the img_url in array 2 into array 1. under the corresponding id.

Finally need to achieve:

Array ( [0] => Array ( [id] => 7 [collection_id] => 1 [prize_num] => 1 [prize_name] => Instant discount of 20 yuan [total] => 10 ,**[url_img]=> /upload/business/1476342419.png** ) [1] => Array ( [id] => 8 [collection_id] => 1 [prize_num] => 2 [ prize_name] => 20% off the entire order [total] => 20,**[url_img]=> /upload/business/1476348963.jpg**)

[Solved] PHP array merging problem

Problem solved

<code>先跑第一个循环,在里面跑第二个循环,去第二个数组找符合条件的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);</code>
Copy after login
Copy after login

Reply content:

Problems encountered:
Currently there are two arrays. It is known that in the two arrays, the id in array 1 is equal to the pid in array 2. The problem is to merge the img_url in array 2 into array 1. under the corresponding id.

Finally need to achieve:

Array ( [0] => Array ( [id] => 7 [collection_id] => 1 [prize_num] => 1 [prize_name] => Instant discount of 20 yuan [total] => 10 ,**[url_img]=> /upload/business/1476342419.png** ) [1] => Array ( [id] => 8 [collection_id] => 1 [prize_num] => 2 [ prize_name] => 20% off the entire order [total] => 20,**[url_img]=> /upload/business/1476348963.jpg**)

[Solved] PHP array merging problem

Problem solved

<code>先跑第一个循环,在里面跑第二个循环,去第二个数组找符合条件的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);</code>
Copy after login
Copy after login

Seeing that you have solved it yourself, I will give you another method

<code>/**
 * 从多维数组中抽取一列'img_url'组成新数组, 并使用多维数组中的id作为key
 * 当然你也可以不用array_column自己通过foreach拼接这个数组
 */
$idImgMap = array_column($shopImageName, 'img_url', 'id'); 
foreach ($shopPrizeName as &$value) {
    $value['img_url'] = $idImgMap[$value['id']];
}</code>
Copy after login

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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!