我正在尝试映射查询以向查询添加一些自定义键值。如果有相同的ID,则将其标记为一批。
例如我的查询(集合)如下:
[
{
ID: "5835910",
TrackingNr: "06215068123019",
TrackingURL: "http://exmaple-website?trackingnr=06215068123019"
},
{
ID: "5835910",
TrackingNr: "06215068123020",
TrackingURL: "http://exmaple-website?trackingnr=06215068123020"
},
{
ID: "5860771",
TrackingNr: "06215068129534",
TrackingURL: "http://exmaple-website?trackingnr=06215068129534"
},
{
ID: "5860771",
TrackingNr: "06215068129535",
TrackingURL: "http://exmaple-website?trackingnr=06215068129535"
},
....
]
我的结果应该是这样的:
[
{
Shipment: 1
ID: "5835910",
TrackingNr: "06215068123019",
TrackingURL: "http://exmaple-website?trackingnr=06215068123019"
},
{
Shimpent: 1
ID: "5835910",
TrackingNr: "06215068123020",
TrackingURL: "http://exmaple-website?trackingnr=06215068123020"
},
{
Shimpent: 2
ID: "5860771",
TrackingNr: "06215068129534",
TrackingURL: "http://exmaple-website?trackingnr=06215068129534"
},
{
Shipment: 2
ID: "5860771",
TrackingNr: "06215068129535",
TrackingURL: "http://exmaple-website?trackingnr=06215068129535"
},
....
]
我尝试将我的查询广告的运货键映射到数组,并为该值设置一个计数器。但它没有按我预期的那样工作。我的计数器不正确计数。
$counter = 1;
$tracking = $tracking->map(function($item, $key) use ($tracking, $counter) {
if(isset($tracking[$key + 1])) {
if($item->BelID != $tracking[$key + 1]->BelID) {
$counter = $counter + 1;
}
}
return [
'Shipment' => $counter,
'ID' => $item->BelID,
'TrackingNr' => $item->TrackingNr,
'TrackingURL' => $item->TrackingURL
];
});
return $tracking;
答案 0 :(得分:0)
您必须允许匿名函数中的变量通过在&
之前添加$
来进行更改:
use ($tracking, &$counter)