2017-04-19 6 views
0

Hallo Zur Zeit arbeite ich mit einigen multidimentional Array-Operationen mit Looping.Wie in Funktion Schleifen multidimentional Array übergeben

Eigentlich muss i multidimentional Array übergeben zu funktionieren, und in dieser Funktion müssen i ganze Array-Daten.

Aber Problem hier ist, ich bin nur einzelnes Element des Arrays nicht alle Elemente des Arrays in Funktion zu bekommen.

Unter dem Code, ich versuche haben:

foreach($sp_data as $sp_product_data) // this loop is for another purpose 
       { 

        $sp_product_details = (array)$sp_product_data; 
        $product_links = array(array(
        'sku' => $gp_sku, 
        'link_type' => 'associated', 
        'linked_product_sku' => $sp_product_details['sku'], 
        'linked_product_type' => 'simple', 
        'position' => '1',  
       ) 
       ); 
      echo "<pre>"; 
      print_r(product_links); //When i am printing the data its shows me all array items 

     $this->testproduct($product_links); // but when i am pass the data to function and in that function when i displayed data its gives only first array item 
       } 

Meine Funktion:

function testproduct($product_links) 
{ 
echo "<pre>"; 
print_r($product_links); 
} 

In Funktion i nur erstes Element des Arrays immer bin. Bitte helfen Sie, was ich bin fehlt

+1

versuchen array_push verwenden, bevor die Testprodukt Funktion – Akintunde007

+1

@Manthan Aufruf Können Sie bitte Ihre Post mit den erwarteten Ausgang aktualisieren? –

Antwort

1

Für ganze Reihe zeigt Ihnen dieses Verwenden Sie dieses Format

$product_links[]=array(
       'sku' => $gp_sku, 
       'link_type' => 'associated', 
       'linked_product_sku' => $sp_product_details['sku'], 
       'linked_product_type' => 'simple', 
       'position' => '1',  
       ); 
}#end of foreach loop 
#now call the testproduct function. THis line must be outside the loop 

$this->testproduct($product_links); 
+0

rufen Sie auch, dass „Testprodukt“ außerhalb der Schleife – Shaniawan

+0

Dank seiner Werke wie Charme !! kurz und einfach. Nochmals vielen Dank –

1

Verwenden array_push für dieses zu tun haben, also sollte der Code wie folgt sein

$custom_product_data = array(); 
foreach($sp_data as $sp_product_data) { 
    $sp_product_details = (array)$sp_product_data; 
    $product_links = array(
     'sku' => $gp_sku, 
     'link_type' => 'associated', 
     'linked_product_sku' => $sp_product_details['sku'], 
     'linked_product_type' => 'simple', 
     'position' => '1',  
); 

    if(is_array($product_links)) { 
     array_push($custom_product_data, $product_links); 
    } 
    } 

    $this->testproduct($custom_product_data); 

HINWEIS

nicht schmieden t, um die Testproduktfunktion außerhalb der Schleife zu verwenden. Sonst gibt es kein Ergebnis.

+0

Vielen Dank für die Hilfe :) –

Verwandte Themen