2016-07-25 8 views
0

値が一致する場合、キーの比較に基づいて2つの異なる配列を結合しようとしています。そうでない場合、結合は行われません。PHPはループ内の特定のキーが一致する場合に結合配列を結合します

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 
) 

目標車両配列[車両]と特殊な配列[車両]上に結合することである:ここ

しかし、両方のアレイは、約1,000アイテムであってもよい、で開始する各配列の短い例であり

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

あなたが使用することはできません 'array_merge()'ただ1つの配列の要素をマージします。入力配列の内容全体を毎回出力配列の1つの要素に貼り付けることになります。 –

+0

'array_merge()'は動作しないので、呼び出す代替関数は何でしょうか? – proph3t

+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

関連する問題