如果值匹配,我正在根据键的比较组合2个不同的数组,否则不会发生组合。
以下是每个数组开始的简短示例,但两个数组都可能是大约1,000个项目:
Vehicle Array
(
[vehicle] => 2016 Ford Transit 250 Cargo Van
[stockno] => 153721
[msrp] => 32195
[price] => 32195
[payment] => 359
[type] => New
[bodystyle] => Van
[mileage] => 6
[year] => 2016
[make] => Ford
[model] => Transit 250
[trim] => Cargo Van
),(
[vehicle] => 2016 Ford F150 XLT
[stockno] => 153723
[msrp] => 36195
[price] => 36195
[payment] => 429
[type] => New
[bodystyle] => Truck
[mileage] => 6
[year] => 2016
[make] => Ford
[model] => F150
[trim] => XLT
)
Special Array
(
[vehicle] => 2016 Ford Transit 250 Cargo Van
[store] => Baxter Ford
[offertype] => $ Off MSRP
[offervalue] => Up to $10,000
[disclaimer] => *Valid on in-stock models. Based on stock #161621. Tax, title and license extra. With approved credit. Includes hail sale savings. See dealer for details. Offer expires 8\/1\/16.
[expires] => 8/1/16
),(
[vehicle] => 2016 Ford Mustang Premium
[store] => Baxter Ford
[offertype] => $ Off MSRP
[offervalue] => Up to $4,000
[disclaimer] => *Valid on in-stock models. Based on stock #163421. Tax, title and license extra. With approved credit. Includes hail sale savings. See dealer for details. Offer expires 8\/1\/16.
[expires] => 8/1/16
)
目标是结合车辆阵列[车辆]和特殊阵列[车辆]来创建这样的阵列:
Combined Array
(
[vehicle] => 2016 Ford Transit 250 Cargo Van
[stockno] => 153721
[msrp] => 32195
[price] => 32195
[payment] => 359
[type] => New
[bodystyle] => Van
[mileage] => 6
[year] => 2016
[make] => Ford
[model] => Transit 250
[trim] => Cargo Van
[store] => Baxter Ford
[offertype] => $ Off MSRP
[offervalue] => Up to $10,000
[disclaimer] => *Valid on in-stock models. Based on stock #161621. Tax, title and license extra. With approved credit. Includes hail sale savings. See dealer for details. Offer expires 8\/1\/16.
[expires] => 8/1/16
),(
[vehicle] => 2016 Ford F150 XLT
[stockno] => 153723
[msrp] => 36195
[price] => 36195
[payment] => 429
[type] => New
[bodystyle] => Truck
[mileage] => 6
[year] => 2016
[make] => Ford
[model] => F150
[trim] => XLT
)
这似乎非常简单,但我似乎错过了一些东西。我尝试过这样的嵌套foreach:
foreach ($vehicleArr as $v) {
foreach ($specialArr as $s) {
if ($v['vehicle'] === $s['vehicle']) {
$freshArr[] = array_merge($v, $s);
} else {
$freshArr[] = $v;
}
}
}
这会造成大量内存泄漏并导致脚本死亡。
提前致谢。
答案 0 :(得分:1)
您的代码问题:新的数组大小变为n * m长度。
我的工作实例
$vehicleList = [
[ 'vehicle' => '2016 Ford Transit 250 Cargo Van',
'stockno' => '153721'],
[ 'vehicle' => '2016 Ford F150 XLT',
'stockno' => '153723']
];
$spectialList = [
[ 'vehicle' => '2016 Ford Transit 250 Cargo Van',
'store' => 'Baxter Ford'],
[ 'vehicle' => '2016 Ford Mustang Premium',
'store' => 'Baxter Ford']
];
$newVehicleList = $vehicleList;
// let's change new array using reference
foreach ($newVehicleList as &$vehicle) {
foreach ($spectialList as $special) {
if ($vehicle['vehicle'] == $special['vehicle']) {
//just change value wihtout creationg new one
$vehicle = array_merge($vehicle, $special);
}
}
}
// to make sure that there will no be any changes
unset($vehicle);
var_dump($newVehicleList);
关于foreach中的参考,您可以在那里阅读:http://php.net/manual/ru/control-structures.foreach.php