SMARTY:验证值是否在多维数组中

时间:2011-12-06 15:26:13

标签: arrays smarty

我有这个multidimensionnal数组smarty:

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

结果如下:

  • Color Blue Disk space 16Go
  • Color Blue Disk space 8Go
  • 磁盘空间16Go颜色黄色
  • 磁盘空间8Go颜色黄色
  • 彩色金属磁盘空间16Go
  • 彩色金属磁盘空间8Go
  • Color Black Disk space 8Go
  • Color Orange Disk space 16Go
  • Color Orange Disk space 8Go
  • Color Pink Disk space 16Go
  • Color Pink Disk space 8Go
  • Color Green Disk space 16Go
  • Color Green Disk space 8Go
  • 磁盘空间16Go Color Purple
  • 磁盘空间8Go Color Purple
  • Color Black Disk space 16Go

不,我想仅在一行包含“蓝色”时才显示结果。 此数组将不会显示,因为他具有属性name = Orange:

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 个答案:

答案 0 :(得分: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}