Shopify中的_array()中是否有PHP的替代方案?

Shopify中的_array()中是否有PHP的替代方案?,shopify,liquid,liquid-layout,Shopify,Liquid,Liquid Layout,我尝试了很多东西,但没有得到我想要的 下面是我在Shopify中寻找的阵列类型的示例 $array['bag']=2$数组['shoes']=3$数组['xyz']=6 下面是我在shopify中查找数组变量的内容和方式示例 在哪里 包、鞋、xyz 是产品类型吗 和2,3,6 是为特定产品类型添加的产品数 我知道用PHP很容易,但不知道如何用Shopify液态代码 根据,无法初始化数组。但是,可以使用创建一维数组。不能使用此命令创建关联数组。但是,作为一种解决方法,使用两个长度相同的数组,其中两

我尝试了很多东西,但没有得到我想要的

下面是我在Shopify中寻找的阵列类型的示例

$array['bag']=2$数组['shoes']=3$数组['xyz']=6

下面是我在shopify中查找数组变量的内容和方式示例

在哪里

包、鞋、xyz

是产品类型吗

和2,3,6

是为特定产品类型添加的产品数

我知道用PHP很容易,但不知道如何用Shopify液态代码

根据,无法初始化数组。但是,可以使用创建一维数组。不能使用此命令创建关联数组。但是,作为一种解决方法,使用两个长度相同的数组,其中两个数组中的相同索引指向关联数组的相关键和值。示例代码

  {% assign product_type = "type-1|type-2|type-3" | split: '|' %}
  {% assign product_count = "1|2|3" | split: '|' %}


    {% for p_type in product_type %}
        {{ p_type }}
        {{ product_count[forloop.index0] }}
    {% endfor %}
预期产量

Product Type   Count
type-1           1
type-2           2
type-3           3
对于注释中解释的特定场景,请查看下面的代码和代码注释。我已经使用了示例代码。你可以根据需要进行调整

// declare 2 vars to create strings - that will be converted to arrays later
{% assign product_type = "" %}
{% assign product_count = "" %}

// iterate over line_items in checkout to build product_type string
{% for line_tem in checkout.line_items %}
  // if product_type exists , then skip -- unique product types
  {% if product_type contains line_tem.product.type%}
  {% else %}
    {% assign product_type = product_type | append: '#' | append: line_tem.product.type %}   
  {% endif %}

{% endfor %}

// remove first extra hash and convert to array
{% assign product_type = product_type | remove_first: "#" | split: '#' %}


// iterate over unique product type array generated earlier
{% for product_type_item in product_type %}
// set product count for this product type to zero initially
{% assign total_count = 0 %}
// iterate over all lin items and +1 if same product type
  {% for line_tem in checkout.line_items %}
    {% if product_type_item == line_tem.product.type%}
      {% assign total_count = total_count | plus: 1 %} 
    {% endif %}
  {% endfor %}
  // append count to product count string
  {% assign product_count = product_count | append: '#' | append: total_count %}
{% endfor %}

// remove first extra hash and convert to array
{% assign product_count = product_count | remove_first: "#" | split: '#'%}

{{-product_type-}}

{{-product_count-}}

我想根据产品类型统计产品。意思是,在循环中,我想检查购物车中添加了多少具有相同产品类型的总产品。i、 e.如果带有产品类型袋的产品为2,则$array['bag']=2;如果带有product_类型鞋的产品为3,则$array['shoes']=3;等等那么我如何才能做到无能呢。不是像你在上面提到的那样静态的。嘿@Bilal,这是你提出的一个好主意,对我来说非常有效。非常感谢+1也被接受。