2016-08-04 12 views
0
zu arbeiten

Dies ist der Code:PHP - ein Array erstellen Der Versuch scheint nicht

function create_order_with_custom_products() 
{ 
    $orderGenerator = new OrderGenerator(); 
    $orderGenerator->setCustomer(6907); 

    $orderGenerator->createOrder(array(
     // Add configurable product 
     array(
      'product' => 30151, 
      'qty' => 1 
     ), array(
      'product' => 30150, 
      'qty' => 2 
     ), 
    )); 
} 

Ich habe Array mit einer solchen Struktur zu schaffen:

 array(
      'product' => 30151, 
      'qty' => 1 
     ), array(
      'product' => 30150, 
      'qty' => 2 
     ), 

Ich versuche, ein Array mit erstellen gleiche Struktur wie folgt aus:

foreach($ItemsInCart['productid'] as $key=>$value){ 
    $ProductId = $value; 
    $ProductQty = $ItemsInCart["productqty"][$key]; 
    $product_id = $ProductId; // Replace id with your product id 
    $qty = $ProductQty; // Replace qty with your qty 

    $ItemsId[] = ['product' => $ProductId, 'qty' => $ProductQty]; 
} 

Das gibt mir Ergebnis:

Array ([0] => Array ([Produkt] => 30143 [Menge] => 1) [1] => Array ( [Produkt] => 30144 [Menge] => 2) [2] => Array ([Produkt] => 30145 [Menge] => 3) [3] => Array ([Produkt] => 30146 [Menge] => 4) [4] => Array ([Produkt] = > 30147 [Menge] => 5))

Alles, was ich wissen will, ist, warum dies:

function create_order_with_custom_products() 
{ 
    $orderGenerator = new OrderGenerator(); 
    $orderGenerator->setCustomer(6907); 

    $orderGenerator->createOrder(array(
     // Add configurable product 
     array(
      'product' => 30151, 
      'qty' => 1 
     ), array(
      'product' => 30150, 
      'qty' => 2 
     ), 
    )); 
} 

nicht das gleiche wie das ist:

function create_order_with_custom_products() 
{ 
    $orderGenerator = new OrderGenerator(); 
    $orderGenerator->setCustomer(6907); 

    $orderGenerator->createOrder(array($ItemsId)); 
} 

Warum der zweite Ansatz nicht funktioniert, wo ist mein Fehler?

+0

Ersetzen Sie $ AUTHEID [] ["product"] = $ ProductId; $ ItemsId [] ["Menge"] = $ ProductQty; 'mit' $ ItemsId [] = ['Produkt' => $ ProductId, 'Menge' => $ ProductQty]; ' –

+0

Ich habe meine Frage über Sie gestellt Bitte schau es dir an. –

Antwort

2

Der einfachste Weg ist

$ItemsId = []; //declaration 
$ItemsId [] = ["product"=>30151,"qty"=>1]; //pusing an array with key 'product' & 'qty' to $ItemsId 

UPDATE

Diese Linie

$orderGenerator->createOrder(array($ItemsId)); 

nicht, weil $ItemsId arbeitet, ist bereits ein Array und Sie setzen diese in ein neues Array mit dies array($ItemsId). Dies wird wie folgt aussehen

Array([0] => 
    Array ([0] => 
      Array(
       [0] =>Array ([product] => 30143,[qty] => 1) 
       [1] => Array ([product] => 30144,[qty] => 2) 
       [2] => Array ([product] => 30145,[qty] => 3) 
       [3] => Array ([product] => 30146,[qty] => 4) 
       [3] => Array ([product] => 30147,[qty] => 5) 
      ) 
    ) 
) 

Aber die erwartete Array aussehen sollte

 Array ([0] => 
      Array(
        [0] =>Array ([product] => 30143,[qty] => 1) 
        [1] => Array ([product] => 30144,[qty] => 2) 
        [2] => Array ([product] => 30145,[qty] => 3) 
        [3] => Array ([product] => 30146,[qty] => 4) 
        [3] => Array ([product] => 30147,[qty] => 5) 
      ) 
     ) 

dieses Problem zu überwinden, nur

ändern
$orderGenerator->createOrder(array($ItemsId)); 

Um

$orderGenerator->createOrder($ItemsId); 
+0

Ich habe meine Frage aktualisiert, können Sie es bitte überprüfen? –

+0

Ich glaube, Sie sprechen über diese Zeile '$ orderGenerator-> createOrder (Array ($ ItemsId));' – jonju

+0

Ja, ich verstehe nicht, warum es nicht funktioniert .. –

0

Verwenden einen Zähler, versichert, dass beide Unterelemente des Arrays teilen sich den gleichen Index. Wie folgt:

$i = 0; 

foreach(...) { 
... 
$ItemsId[$i]["product"] = $ProductId;    
$ItemsId[$i]["qty"] = $ProductQty; 
... 
$i++; 
} 
+0

Ich habe meine Frage aktualisiert, können Sie es bitte überprüfen? –

+0

Setzen Sie '$ ItemsId' in der Methode? In Ihrem Beispiel ist es nicht gesetzt – rbr94

+0

Ich bin mir nicht sicher, können Sie bitte Ihre Antwort aktualisieren? –

Verwandte Themen