2016-09-02 62 views
1

小東西卡住....我可以抓住{{value.title}}顯示正常,但獲取內部數組值不顯示,不知道我做錯了什麼這裏......以前從未使用樹枝,所以可能只是我缺少一些東西。使用樹枝模板引擎訪問陣列

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 
    } 

嫩枝

<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> 

如果我輸出vaule.variants它輸出

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

但不能似乎得到條形碼和SKU顯示任何想法?

+0

嗨@詹姆斯,我希望我的兩個解決方案的腳ÿ我們需要 – Matteo

回答

1

變體屬性是一個數組,你應該在它週期,例如:

{% 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

也許,看到你的代碼,不如你改變你的數據結構(你需要永遠是產品的標題顯示),並簡化了樹枝代碼,例如我建議你如下:

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 

嫩枝

<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