2016-08-05 12 views
-1

Ich möchte zwei Arrays auf demselben Schlüssel zusammenführen. Ich habe mein Bestes versucht, aber ich bin nicht in der Lage Erfolg zu haben. Ich habe beide Arrays unten hinzugefügt. Bitte überprüfen Sie beide Arrays im Detail und helfen Sie mir, sie mit dem gleichen Schlüssel zusammenzuführen.Merge 2 Array gleichen Schlüssel

Hier ist das erste Array:

Array 
(
    [1] => Array 
     (
      [costprice1] => 500 
      [margin1] => 20 

     ) 

    [2] => Array 
     (
      [costprice2] => 600 
      [margin2] => 15 

     ) 

    [3] => Array 
     (
      [costprice3] => 700 
      [margin3] => 25 

     ) 

) 

Hier ist die 2-Array:

Array 
(
    [1] => Array 
     (

      [entityType1] => Products1 
     ) 
    [2] => Array 
     (
      [entityType2] => Products2 
     ) 
    [3] => Array 
     (
      [entityType3] => Products3 
     ) 
) 

ich möchte, wie die Array müssen Sie Vorschlag mich

Array 
(
    [1] => Array 
     (

      [entityType1] => Products1 
      [costprice1] => 500 
      [margin1] => 20 
     ) 
    [2] => Array 
     (
      [entityType2] => Products2 
      [costprice2] => 600 
      [margin2] => 15 
     ) 
    [3] => Array 
     (
      [entityType3] => Products3 
      [costprice3] => 700 
      [margin3] => 25 
     ) 
) 

bitte hilf mir wie dies zu verschmelzen zwei Array

+0

Können Sie den Code zur Verfügung stellen zu zeigen, was Ihr bester Versuch war? StackOverflow ist mehr für Menschen mit bestimmten Fragen/Problemen als für allgemeine Antworten (und die Leute werden Ihre Frage für solche ablehnen), also, wenn Sie einen Kontext bereitstellen können, wo Sie dazu kamen, würde uns helfen. –

Antwort

1

Versuche:

foreach($array1 as $key => $value) { 
    $array1[$key]['entityType'.$key] = $array2[$key]['entityType'.$key]; 
} 
print_r($array1); 
0
<?php 

$array1 = [ 
    1 => [ 
    'costprice1' => 500, 
    'margin1' => 20 
    ], 
    2 => [ 
    'costprice2' => 600, 
    'margin2' => 15 
    ], 
    3 => [ 
    'costprice2' => 700, 
    'margin2' => 25 
    ], 
]; 

$array2 = [ 
    1 => ['entityType1' => 'Products1'], 
    2 => ['entityType2' => 'Products2'], 
    3 => ['entityType3' => 'Products3'], 
]; 

array_walk($array2, function(&$v, $k)use($array1){ 
    $v = array_merge($v, $array1[$k]); 
}); 

print_r($array2); 

Ausgang:

Array 
(
    [1] => Array 
     (
      [entityType1] => Products1 
      [costprice1] => 500 
      [margin1] => 20 
     ) 

    [2] => Array 
     (
      [entityType2] => Products2 
      [costprice2] => 600 
      [margin2] => 15 
     ) 

    [3] => Array 
     (
      [entityType3] => Products3 
      [costprice2] => 700 
      [margin2] => 25 
     ) 

) 

https://eval.in/618561

+0

Vielen Dank Bert für Ihren Vorschlag funktioniert, aber Schlüssel sollte starten 1 –

+0

bert Ich brauche Array-Schlüssel Start mit 1 –

+0

Sie können array_unshift() mit einem leeren Array verwenden. Dies ist der Grund dafür, dass das erste Array leer ist. – Peter