Problems with the "resolved" PHP array merge

Source: Internet
Author: User

Problems encountered:
There are currently two arrays, known to be two arrays, and the IDs in array 1 equals the PID in array 2, the problem is to merge the Img_url in array 2 into the corresponding IDs in array 1.

Ultimately, you need to implement:

Array ( [0] => Array ( [id] => 7 [collection_id] => 1 [prize_num] => 1 [prize_name] => 立减20元 [total] => 10 ,**[url_img]=> /upload/business/1476342419.png** ) [1] => Array ( [id] => 8 [collection_id] => 1 [prize_num] => 2 [prize_name] => 全单8折 [total] => 20,**[url_img]=> /upload/business/1476348963.jpg**)

The problem is resolved

先跑第一个循环,在里面跑第二个循环,去第二个数组找符合条件的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 encountered:
There are currently two arrays, known to be two arrays, and the IDs in array 1 equals the PID in array 2, the problem is to merge the Img_url in array 2 into the corresponding IDs in array 1.

Ultimately, you need to implement:

Array ( [0] => Array ( [id] => 7 [collection_id] => 1 [prize_num] => 1 [prize_name] => 立减20元 [total] => 10 ,**[url_img]=> /upload/business/1476342419.png** ) [1] => Array ( [id] => 8 [collection_id] => 1 [prize_num] => 2 [prize_name] => 全单8折 [total] => 20,**[url_img]=> /upload/business/1476348963.jpg**)

The problem is resolved

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

I saw you fix it, and I'll give you a way.

/** * 从多维数组中抽取一列'img_url'组成新数组, 并使用多维数组中的id作为key * 当然你也可以不用array_column自己通过foreach拼接这个数组 */$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), your is O (n^2), so this performance will be a bit 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.