2016-09-02 3 views
1

少しでもつまらない... {{value.title}}をつかむことができますが、内部配列の値が表示されていて、何が間違っているのかわかりませんここに...前に小枝を使ったことがないので、何かが欠けているかもしれません。twigテンプレートエンジンを使って配列にアクセスする

PHP

$product_array = array(); 

    foreach ($shopifyProducts as $product) { 
     $item = array(
      "title" => $product["title"], // product title 
      "variants" => array() // for now an empty array, we will fill it in the next step 
    ); 
     foreach($product["variants"] as $variant) { 
      $item["variants"][] = array(
       "barcode" => $variant["barcode"], // product barcode 
       "sku" => $variant["sku"] // product SKU 
     ); 
     } 
     $product_array[] = $item; // now we add the item with all the variants to the array with all the products 
    } 

TWIG

<div class="table-responsive"> 
    <table class="table"> 
     <thead> 
     <th>Title</th> 
     <th>Barcode</th> 
     <th>SKU</th> 
     </thead> 
     <tbody> 
     <tr ng-repeat="value in remote_data"> 
      <td>{{ value.title }}</td> 
      <td>{{ value.variants.barcode }}</td> 
      <td>{{ value.variants.sku }}</td> 
      <td>{{ value.variants }}</td> 
     </tr> 
     </tbody> 
    </table> 
</div> 

I出力vaule.variantsもしそれが

[{"barcode":"5060315468600","sku":"PLU#1"}] 

を出力しかし、任意のアイデアを表示するためのバーコードとSKUを得るように見えるカント?

{% for variant in value.variants %} 

<td>{{ value.title }}</td> 
<td>{{ variant.barcode }}</td> 
<td>{{ variant.sku }}</td> 

{% endfor %} 

Here a working example with the sample data provided:バリアント属性

+0

こんにちは@James、私は2ソリューションの足yと願っています私たちの必要性 – Matteo

答えて

1

は、例として、その中にあなたがすべきサイクル、配列です。あなたのコード見おそらく

、あなたは(あなたは、常に製品のタイトルを表示する必要がある)あなたのデータの構造を変更し、小枝コードを簡素化する、例として、私はあなたに次のことを提案する方がよい:

PHP

$product_array = array(); 

foreach ($shopifyProducts as $product) { 

    foreach($product["variants"] as $variant) { 
     $product_array[] = array(
      'title' => $product["title"], // always display the product title 
      "barcode" => $variant["barcode"], // product barcode 
      "sku" => $variant["sku"] // product SKU 
     ); 
    }// end foreach 
}// end foreach 

TWIG

<div class="table-responsive"> 
    <table class="table"> 
     <thead> 
     <th>Title</th> 
     <th>Barcode</th> 
     <th>SKU</th> 
     </thead> 
     <tbody> 
     <tr ng-repeat="value in remote_data"> 
      <td>{{ value.title }}</td> 
      <td>{{ value.barcode }}</td> 
      <td>{{ value.sku }}</td> 
     </tr> 
     </tbody> 
    </table> 
</div> 
+0

ありがとうございました:)治療を働いた! – James

+0

こんにちは@ジェームズあなたは歓迎です! – Matteo

関連する問題