Php 如何使用foreach遍历两个长度相同的集合

Php 如何使用foreach遍历两个长度相同的集合,php,laravel,collections,laravel-5,foreach,Php,Laravel,Collections,Laravel 5,Foreach,如果我们想对两个集合的某些属性进行合并,假设它们具有相同的长度,例如: 系列1: $collection1 = Collection { ▼ #items: array:2 [ ▼ 0 => Item { ▼ +id: 1 +first_name: 'first_name 1' +last_name: 'first_name 1' +nbr_hours: 9 } 1 => Item { ▼ +id

如果我们想对两个集合的某些属性进行合并,假设它们具有相同的长度,例如:

系列1:

$collection1 = Collection { ▼
  #items: array:2 [ ▼
    0 => Item { ▼
      +id: 1
      +first_name: 'first_name 1'
      +last_name: 'first_name 1'
      +nbr_hours: 9
    }
    1 => Item { ▼
      +id: 2
      +first_name: 'first_name 2'
      +last_name: 'first_name 2'
      +nbr_hours: 10
    }
  ]
}
$collection2 = Collection { ▼
  #items: array:2 [ ▼
    0 => Item { ▼
      +id: 1
      +first_name: 'first_name 1'
      +last_name: 'first_name 1'
      +nbr_hours: 10
    }
    1 => Item { ▼
      +id: 2
      +first_name: 'first_name 2'
      +last_name: 'first_name 2'
      +nbr_hours: 12
    }
  ]
}
系列2:

$collection1 = Collection { ▼
  #items: array:2 [ ▼
    0 => Item { ▼
      +id: 1
      +first_name: 'first_name 1'
      +last_name: 'first_name 1'
      +nbr_hours: 9
    }
    1 => Item { ▼
      +id: 2
      +first_name: 'first_name 2'
      +last_name: 'first_name 2'
      +nbr_hours: 10
    }
  ]
}
$collection2 = Collection { ▼
  #items: array:2 [ ▼
    0 => Item { ▼
      +id: 1
      +first_name: 'first_name 1'
      +last_name: 'first_name 1'
      +nbr_hours: 10
    }
    1 => Item { ▼
      +id: 2
      +first_name: 'first_name 2'
      +last_name: 'first_name 2'
      +nbr_hours: 12
    }
  ]
}
例如,我们如何在同一时间循环它们,并在
nbr\u hours
属性中进行处理,以便输出如下:

$nested_collection = Collection { ▼
  #items: array:2 [ ▼
    0 => Item { ▼
      +id: 1
      +first_name: 'first_name 1'
      +last_name: 'first_name 1'
      +nbr_hours: 19
    }
    1 => Item { ▼
      +id: 2
      +first_name: 'first_name 2'
      +last_name: 'first_name 2'
      +nbr_hours: 22
    }
  ]
}
首先是需求 需要使用foreach循环遍历集合的解决方案

以相同的顺序处理具有相同项目的两个集合 如果每个集合的项目计数和排序相同,则可以使用遍历的每个元素的索引从另一个集合获取“合作伙伴元素”

foreach($collection1 as $index => $item){
    $hours = $item->nbr_hours . $collection2[$index]->nbr_hours;
}
$collection2 = $collection2->keyBy('id'); //Change indexes to model ids 
$collection1 = $collection1->keyBy('id'); //For both collections
foreach($collection1 as $index => $item){
    $hours = $item->nbr_hours . $collection2->get($index)->nbr_hours;
}

不考虑排序和项目计数的方法 如果集合元素的顺序不同,或者集合项计数不同,则查询看起来会更复杂一些。在这种情况下,您需要查询id相同的元素。为了实现这一点,我们可以将集合索引更改为包含模型的id。现在我们可以再次使用一个元素的索引在另一个集合中查找对应的元素

foreach($collection1 as $index => $item){
    $hours = $item->nbr_hours . $collection2[$index]->nbr_hours;
}
$collection2 = $collection2->keyBy('id'); //Change indexes to model ids 
$collection1 = $collection1->keyBy('id'); //For both collections
foreach($collection1 as $index => $item){
    $hours = $item->nbr_hours . $collection2->get($index)->nbr_hours;
}

这是一个关于数组的示例,您可以做些什么来实现这一点-

$c1 = [
['id' => 1, 'hrs' => 9],
['id' => 2, 'hrs' => 12],
];

$c2 = [
['id' => 1, 'hrs' => 10],
['id' => 2, 'hrs' => 11],
];

foreach($c1 as &$c) {
    // get the index for the id from the 2nd array
    $c2_index = array_search($c['id'], array_column($c2, 'id'));
    // Increment the value
    $c['hrs'] += $c2[$c2_index]['hrs'];
}

如果要使用收集方法,可以使用以下方法:

    $zip = $c1->zip($c2->toArray());
    $m = $zip->map(function($items, $key) {
        $sum = 0;
        foreach ($items as $k => $item) {
            $sum += $item['nbr_hours'];
        }
        $r = $items[$k];
        $r['nbr_hours'] = $sum;
        return $r;
    });
基本上,
zip
方法将在同一个键中“合并”数组。然后,如果使用
map
方法,可以迭代主键。之后,可以将这两个数组作为项处理。因此,您可以执行所需的任何类型的操作,然后返回一个新数组作为结果。

您可以使用以下方法:

$nested_collection  = $collection1->map(function ($item, $key) use($collection2) {
    $item->nbr_hours = $item->nbr_hours + $collection2->get($key)->nbr_hours;
    return $item;
});
注意-如果值的顺序不同,则可以使用方法

values方法返回一个新集合,其中键重置为连续整数


对@AmitGupta答案的一个小修改。如果集合2需要由
$item->id
匹配。试试下面的答案。:)


让我知道:)

如果这些值的顺序不一样会怎样。然后可能会在@SougataBoseOnce visit创建一个更复杂的查询