2011-12-06 45 views
1

我在智者此multidimensionnal數組:SMARTY:驗證一個值是在一個multimensionnal陣列

attribute => Array (16) 
    25 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Blue" 
     id_product_attribute => "25" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "16Go" 
     id_product_attribute => "25" 
    26 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Blue" 
     id_product_attribute => "26" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "8Go" 
     id_product_attribute => "26" 
    27 => Array (2) 
    0 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "16Go" 
     id_product_attribute => "27" 
    1 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Yellow" 
     id_product_attribute => "27" 
    28 => Array (2) 
    0 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "8Go" 
     id_product_attribute => "28" 
    1 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Yellow" 
     id_product_attribute => "28" 
    29 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Metal" 
     id_product_attribute => "29" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "16Go" 
     id_product_attribute => "29" 
    30 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Metal" 
     id_product_attribute => "30" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "8Go" 
     id_product_attribute => "30" 
    32 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Black" 
     id_product_attribute => "32" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "8Go" 
     id_product_attribute => "32" 
    33 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Orange" 
     id_product_attribute => "33" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "16Go" 
     id_product_attribute => "33" 
    34 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Orange" 
     id_product_attribute => "34" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "8Go" 
     id_product_attribute => "34" 
    35 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Pink" 
     id_product_attribute => "35" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "16Go" 
     id_product_attribute => "35" 
    36 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Pink" 
     id_product_attribute => "36" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "8Go" 
     id_product_attribute => "36" 
    39 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Green" 
     id_product_attribute => "39" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "16Go" 
     id_product_attribute => "39" 
    40 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Green" 
     id_product_attribute => "40" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "8Go" 
     id_product_attribute => "40" 
    41 => Array (2) 
    0 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "16Go" 
     id_product_attribute => "41" 
    1 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Purple" 
     id_product_attribute => "41" 
    42 => Array (2) 
    0 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "8Go" 
     id_product_attribute => "42" 
    1 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Purple" 
     id_product_attribute => "42" 
    31 => Array (2) 
    0 => Array (3) 
     public_group_name => "Color" 
     attribute_name => "Black" 
     id_product_attribute => "31" 
    1 => Array (3) 
     public_group_name => "Disk space" 
     attribute_name => "16Go" 
     id_product_attribute => "31" 

我可以顯示所有數組由於該解決方案link

{foreach from=$product.attribute key='id_attribute' item='attribute'} 

<li> 

    {foreach from=$attribute key='id_property' item='property'} 
     {$property.public_group_name} 
     {$property.attribute_name} 
    {/foreach} 

</li> 

{/的foreach }

結果是這樣的:

  • 顏色藍色磁盤空間16Go
  • 顏色藍色磁盤空間8Go
  • 磁盤空間16Go顏色黃色
  • 磁盤空間8Go顏色黃色
  • 彩色金屬磁盤空間16Go
  • 彩色金屬磁盤空間8Go
  • 顏色黑色磁盤空間8Go
  • 顏色橙色磁盤空間16Go
  • 顏色橙色磁盤空間8Go
  • 顏色粉紅色的磁盤空間16Go
  • 顏色粉色磁盤空間8Go
  • 顏色綠色磁盤空間16Go
  • 顏色綠色磁盤空間8Go
  • 磁盤空間16Go紫色
  • 磁盤空間8Go紫色
  • 顏色黑色磁盤空間16Go

NOw,我想僅在包含「藍色」的行顯示結果。 此數組不會被顯示,因爲他有屬性名=橙:

33 => Array (2) 
     0 => Array (3) 
      public_group_name => "Color" 
      attribute_name => "Orange" 
      id_product_attribute => "33" 
     1 => Array (3) 
      public_group_name => "Disk space" 
      attribute_name => "16Go" 
      id_product_attribute => "33" 

我該怎麼辦呢? 使用in_array,我可以驗證一個值是否在多維數組中?

THX

回答

1

我想這可能做的工作:

{foreach from=$product.attribute key='id_attribute' item='attribute'} 
{* 
    Test if the first or second element of this array has 
    public_group_name "Color" 
    attribute_name "Blue" 
*} 
{if ($attribute[0].public_group_name == "Color" && $attribute[0].attribute_name == "Blue") || ($attribute[1].public_group_name == "Color" && $attribute[1].attribute_name == "Blue")} 
<li> 

    {foreach from=$attribute key='id_property' item='property'} 
     {$property.public_group_name} 
     {$property.attribute_name} 
    {/foreach} 

</li> 
{/if} 
{/foreach} 
+0

的PB是顏色屬性並不總是首先在:數組[0]。可能與: {如果$屬性[0] .public_group_name ==「藍色」或$屬性[1]等...} – prestarocket

+0

@adokara哦,是的,我明白了。我會在上面編輯。 –

相關問題