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