2016-11-24 5 views
0

Ich habe ein Array wie https://3v4l.org/VrIDeaus einem Array

$arr = array(
'slidelink' => 'presentation.pptx', 
'productid' => array(1,3), 
'order' => 2, 
'class_id' => array(1,2), 
'currency_id' => array(1,2), 
'presentation_type' => 1, 
'distribution' => 0, 

);

Und ich möchte alle möglichen Ausgänge davon zu einem neuen Array zu erzeugen, so dass in diesem Fall 8 Zeilen wie:

$new_arr = array (
0 => array(presentation.pptx, 1, 2, 1, 1, 1, 0), 
1 => array(presentation.pptx, 1, 2, 1, 2, 1, 0), 
2 => array(presentation.pptx, 1, 2, 2, 1, 1, 0), 
3 => array(presentation.pptx, 1, 2, 2, 2, 1, 0), 
4 => array(presentation.pptx, 3, 2, 1, 1, 1, 0), 
5 => array(presentation.pptx, 3, 2, 1, 2, 1, 0), 
6 => array(presentation.pptx, 3, 2, 2, 1, 1, 0), 
7 => array(presentation.pptx, 3, 2, 2, 2, 1, 0) 
); 

Wie kann ich das tun?

Danke!

+0

Was haben Sie bisher versucht? – Dekel

Antwort

1

Verwenden Sie verschachtelte Schleifen für alle Array-Elemente, die Sub-Arrays sind.

Um die Möglichkeit zu umgehen, dass das Element möglicherweise kein Array ist, können Sie es mithilfe einer Umwandlung in ein Array konvertieren.

$new_arr = array(); 
foreach ((array)$arr['slidelink'] as $s) { 
    foreach ((array)$arr['productid'] as $pid) { 
     foreach ((array)$arr['order'] as $o) { 
      foreach ((array)$arr['class_id'] as $cid) { 
       foreach ((array)$arr['currency_id'] as $curr) { 
        foreach ((array)$arr['presentation_type'] as $p) { 
         foreach ((array)$arr['distribution'] as $d) { 
          $new_arr[] = array($s, $pid, $o, $cid, $curr, $p, $d); 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

Danke, es hat korrekte Ausgabe, aber nur für das Array zur Verfügung gestellt. Dieses Array würde jedes Mal anders sein, zum Beispiel product_id könnte ein einzelner Wert sein, aber die Distribution wird zu einem Array (1,2). – giker

+0

Das ist ein schreckliches Design. Sie sollten konsistent sein, ob Sie in jedem Element Arrays oder einzelne Werte verwenden. – Barmar

+0

Aber dieses Array ist eine Ausgabe der aufgegliederten Excel-Zeile wie: '3', 'presentation.pptx', '1; 3', '2', '1; 2', '1; 2', '1', '0' – giker

Verwandte Themen