PHP Combine Associative Arrays If Specific Key Matches Inside For-Loop -


i working combine 2 different arrays based on comparison of key if values match, otherwise combine not occur.

here short example of each array begin with, both array 1,000 items:

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] => $10,000   [disclaimer] => *valid on in-stock models. based on stock #161621. tax, title , license extra. approved credit. includes hail sale savings. see dealer details. offer expires 8\/1\/16.   [expires] => 8/1/16 ),(   [vehicle] => 2016 ford mustang premium   [store] => baxter ford   [offertype] => $ off msrp   [offervalue] => $4,000   [disclaimer] => *valid on in-stock models. based on stock #163421. tax, title , license extra. approved credit. includes hail sale savings. see dealer details. offer expires 8\/1\/16.   [expires] => 8/1/16 ) 

the goal combine on vehicle array[vehicle] , special array[vehicle] create array this:

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] => $10,000   [disclaimer] => *valid on in-stock models. based on stock #161621. tax, title , license extra. approved credit. includes hail sale savings. see dealer 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 ) 

this seems extremely simple, seem missing something. have attempted nested foreach so:

foreach ($vehiclearr $v) {   foreach ($specialarr $s) {     if ($v['vehicle'] === $s['vehicle']) {       $fresharr[] = array_merge($v, $s);     } else {       $fresharr[] = $v;     }   } } 

this creates massive memory leak , kills script.

thanks in advance.

problem of code: new array sizes becomes n*m length.

and working example

$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 &$vehicle) {     foreach ($spectiallist $special) {         if ($vehicle['vehicle'] == $special['vehicle']) {             //just change value wihtout creationg new 1             $vehicle = array_merge($vehicle, $special);          }     } } // make sure there no changes unset($vehicle);   var_dump($newvehiclelist); 

about reference in foreach read there: http://php.net/manual/ru/control-structures.foreach.php


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -