2017-05-10 3 views
1

So habe ich Array wie diesePhp-Array-Transformation und Kombination

[ 
    'custid' => [ 
     'customer_number_1' => '20098374', 
     'customer_number_8' => '20098037', 
     'customer_number_15' => '20098297' 
    ], 
    'destid' => [ 
     'destination_numbers_1' => [ 
      (int) 0 => '20024838', 
      (int) 1 => '20041339' 
     ], 
     'destination_numbers_8' => [ 
      (int) 0 => '20008293' 
     ], 
     'destination_numbers_15' => [ 
      (int) 0 => '20016969', 
      (int) 1 => '20022919', 
      (int) 2 => '20025815', 
      (int) 3 => '20026005', 
      (int) 4 => '20027083', 
      (int) 5 => '20045497' 
     ] 
    ] 
] 

Ziel ist cust id mit destid paarweise zu verschmelzen und wie so

[ 
    (int) 0 => [ 
     'user_id' => (int) 1, 
     'sap_customer_id' => '20098374', 
     'sap_destination_id' => '20024838' 
    ], 
    (int) 1 => [ 
     'user_id' => (int) 1, 
     'sap_customer_id' => '20098374', 
     'sap_destination_id' => '20041339', 
    ], 
    (int) 2 => [ 
     'user_id' => (int) 1, 
     'sap_customer_id' => '20098037', 
     'sap_destination_id' => '20008293, 
    ], 
    (int) 3 => [ 
     'user_id' => (int) 1, 
     'sap_customer_id' => '20098297' 
     'sap_destination_id' => '20016969', 
    ], 
... 

ich versucht habe, unten mit Code aussehen sollte , aber ich bekomme die Nummer destination_id als Array und doppelte Kundennummern. Auch ich habe mit Array-Walk versucht, aber das Ergebnis ist das gleiche.

$data = []; 
     foreach ($sap_data['custid'] as $custid) { 
      foreach ($sap_data['destid'] as $destid) { 
      $data[] = [ 
       'user_id' => 1, 
       'sap_customer_id' => $custid, 
       'sap_destination_id' => $destid 
      ]; 
      } 
     } 

Danke für Ihre Hilfe!

+0

Sollte 'user_id' 1 immer im Ergebnis? Just double checking .. – Jeppsen

+0

Nein, aber es wird Fix Nummer aus der Formularansicht gesendet werden – WayOut

Antwort

3

sollten Sie innere Schleife machen in anderer Weise

foreach($sap_data['custid'] as $k => $custid) { 
    // Make destination key 
    $dkey = str_replace('customer_number', 'destination_numbers', $k); 
    // And get array, for example, destination_numbers_1 for customer_number_1 
    foreach ($sap_data['destid'][$dkey] as $destid) { 
    $data[] = [ 
       'user_id' => 1, 
       'sap_customer_id' => $custid, 
       'sap_destination_id' => $destid 
      ]; 
    } 
} 

demo on eval.in