douwen1213 2017-02-22 09:33
浏览 97
已采纳

基于相同的键合并两个不同的数组

I have 2 different arrays

First is

(int) 0 => [
        'id' => (int) 1,
        'file_name' => 'test',
        'imported_by' => 'John',
        'valid_to' => (float) 1767225600000
    ],
    (int) 1 => [
        'id' => (int) 2,
        'file_name' => null,
        'imported_by' => 'John',
        'valid_to' => (float) 1767225600000
    ],

Second one is

(int) 0 => object(App\Model\Entity\Product) {
        'id' => (int) 1,
        'product_cat' => (int) 10,
        'product_type' => (int) 1,
        'average....

I have to push first one into second one based on same key

it shoud look like this one

(int) 0 => object(App\Model\Entity\Product) {
        'id' => (int) 1,
        'product_category_id' => (int) 10,
        'product_vessel_id' => (int) 1,
        'average' => test;
        //content from first array
        'file_name' => 'test',
        'imported_by' => 'John',
        'valid_to' => (float) 1767225600000

I have tryied with array_merge_recursive($firstArray,$secondArray); but that mean adding elements on the end of firstArray

  • 写回答

3条回答 默认 最新

  • doujiao6507 2017-02-22 09:46
    关注
    $array1 = array(...);
    $array2 = array(...);
    
    foreach ( $array1 as $element ) {
      foreach ( $array2 as $object ) {
        if ( $object->id == $element['id'] ) {
             $object->filename = $element['filename'];
             $object->imported_by = $element['imported_by'];
             $object->valid_to = $element['valid_to'];
             break;
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据