Smarty用数组嵌套for-each

时间:2012-11-08 13:54:04

标签: foreach smarty

我在使用Smart for-each循环时遇到了一些麻烦 - 这是我的代码:

{* this example will print out all the values of the $custid array *}
{foreach from=$order_info item=amount}
amount: {$amount}<br>
{/foreach}

所以基本上我只是试图循环并打印来自$ order_info的“金额” - 问题是,它只打印整个数组而不仅仅是“金额”。

这是我从调试控制台获得的数组中减少的项目数:

{$order_info}   Array (86)
order_id => "13"
is_parent_order => "N"
parent_order_id => "0"
company_id => "0"
user_id => "1"
total => "1077.87"
subtotal => "1049.87"
discount => "0.00"
subtotal_discount => "0.00"
payment_surcharge => "0.00"
shipping_ids => "1"
shipping_cost => "28.00"
timestamp => "1352380055"
status => "O"
notes => ""
details => ""
promotions => Array (0)
promotion_ids => ""
title => ""
firstname => "John"
lastname => "Doe"
company => ""
b_title => ""
b_firstname => "John"
b_lastname => "Doe"
b_address => "44 Main street"
b_address_2 => "test"
b_city => "Boston"
b_county => ""
b_state => "MA"
b_country => "US"
b_zipcode => "02134"
b_phone => ""
s_title => ""
s_firstname => "John"
s_lastname => "Doe"
s_address => "44 Main street"
s_address_2 => "test"
s_city => "Boston"
s_county => ""
s_state => "MA"
s_country => "US"
s_zipcode => "02134"
s_phone => ""
phone => ""
fax => ""
url => ""
email => "joe@burford.co.uk"
payment_id => "3"
tax_exempt => "N"
lang_code => "EN"
ip_address => "62.49.144.33"
repaid => "0"
validation_code => ""
localization_id => "0"
payment_method => Array (15)
  payment_id => "3"
  usergroup_ids => "0"
  position => "30"
  status => "A"
  template => "check.tpl"
  processor_id => "0"
  params => ""
  a_surcharge => "0.000"
  p_surcharge => "0.000"
  localization => ""
  payment => "Check"
  description => "Check payment"
  instructions => "Type instructions to pay by visiting ..."
  lang_code => "EN"
  processor => ""
fields => Array (0)
items => Array (2)
  2902390881 => Array (19)
    item_id => "2902390881"
    order_id => "13"
    product_id => "1110"
    product_code => "B0002I9OJS"
    price => "549.99"
    amount => "1"
    extra => Array (9)
      step => "1"
      product_options => Array (0)
      unlimited_download => "N"
      product => "eMachines T2958 Desktop PC (2.66 GHz ..."
      company_id => "0"
      is_edp => "N"
      edp_shipping => "N"
      base_price => "549.99"
      stored_price => "N"
    product => "eMachines T2958 Desktop PC (2.66 GHz ..."
    deleted_product => ""
    discount => "0"
    company_id => "0"
    base_price => "549.99"
    original_price => "549.99"
    cart_id => "2902390881"
    tax_value => "0"
    subtotal => "549.99"
    display_subtotal => "549.99"
    shipped_amount => "0"
    shipment_amount => "1"
  2278796194 => Array (19)
    item_id => "2278796194"
    order_id => "13"
    product_id => "1120"
    product_code => "B00086HZJA"
    price => "499.88"
    amount => "1"
    extra => Array (9)
      step => "1"
      product_options => Array (0)
      unlimited_download => "N"
      product => "HP Pavilion a1000n Desktop PC (Intel ..."
      company_id => "0"
      is_edp => "N"
      edp_shipping => "N"
      base_price => "499.88"
      stored_price => "N"
    product => "HP Pavilion a1000n Desktop PC (Intel ..."
    deleted_product => ""
    discount => "0"
    company_id => "0"
    base_price => "499.88"
    original_price => "499.88"
    cart_id => "2278796194"
    tax_value => "0"
    subtotal => "499.88"
    display_subtotal => "499.88"
    shipped_amount => "0"
    shipment_amount => "1"

所以我真正要做的就是从amount items中获取$order_info - 我尝试了$ order_info.items,但这导致网站崩溃。

非常感谢任何想法。

2 个答案:

答案 0 :(得分:3)

这解决了它:

{foreach from=$order_info.items item=foo} 
     {$foo.product_id} 
{/foreach}

答案 1 :(得分:1)

不会像

那样
{foreach from=$order_info.items item=items}
    {foreach from=$items item=item}
        amount: {$item.amount}<br>
    {/foreach}
{/foreach}

假设我将您的结构理解为$ order_info - &gt; items array - &gt; (项目项目项目),以及具有.amount属性的每个项目。