php - 在液体中创建一个简单的数组
问题描述
我需要按数量制作一个关联的产品数组。如何将这一衬里从 php 转换为液体
foreach(items as item) $product[$item.product_id] += $item.quantity
我想出的最好的就是这个
{% for item in cart.items %}
{% assign product[item.product_id] = 0 | plus product[item.product_id] | plus item.quantity %}
{% endfor %}
解决方案
您不能像在液体中使用其他语言一样创建数组。创建数组的唯一方法是在拆分字符串之后。
所以你不能使用product[item.product_id]
.
您首先需要生成一个字符串,然后通过拆分该字符串来创建数组。
{%- capture items -%}
{%- for line_item in cart.items -%}
{{- line_item.product_id -}}|{{-line_item.quantity-}},
{%- endfor -%}
{%- endcapture -%}
{% assign items_array = items | split: ',' %}
这就是我们捕获输出并将其拆分以创建数组的原因。
推荐阅读
- javascript - 根据用户角色限制点击按钮
- jupyter-notebook - FileUpload ipywidget:如何指定初始目录?
- python - 我不断收到此错误代码,但不知道为什么?只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或
- python - python文件转换为exe上升错误
- groovy - 在 Groovy 中 - 如何比较major.minor.patch 和major minor 的版本?
- go - 调试单个测试时如何提供测试标志?
- php - 使用 $_session 存储具有从 api 检索到的动态数据的变量,以 json 格式在不同的页面中使用
- java - android studio getDownloadUrl() 解析
- java - 结果 JSON 上的 Javax Rest API 日期格式
- javascript - Slick Slider 图片尺寸大于屏幕