Php 如何使用细枝按重量生成随机结果?

Php 如何使用细枝按重量生成随机结果?,php,twig,Php,Twig,目前使用OctoberCMS和Twig从json文件获取动态数据。其基本思想是: 有一个有3个链接的侧边栏 边栏链接是随机生成的 装载 有些链接需要比其他链接更频繁地显示 其他的和一些优先级较低的 下面是一些示例代码,让您了解我要实现的目标: "wiki": { "resources": { "items": [ { "href": "[link]", "h3": "[Title]",

目前使用OctoberCMS和Twig从json文件获取动态数据。其基本思想是:

  • 有一个有3个链接的侧边栏
  • 边栏链接是随机生成的 装载
  • 有些链接需要比其他链接更频繁地显示 其他的和一些优先级较低的
下面是一些示例代码,让您了解我要实现的目标:

"wiki": {
    "resources": {
      "items": [
          {
            "href": "[link]",
            "h3": "[Title]",
            "p": "[Description]",
            "tier": 1
          },
          {
            "href": "[link]",
            "h3": "[Title]",
            "p": "[Description]",
            "tier": 3
          },
          {
            "href": "[link]",
            "h3": "[Title]",
            "p": "[Description]",
            "tier": 2
          },
          {
            "href": "[link]",
            "h3": "[Title]",
            "p": "[Description]",
            "tier": 2
          },
          {
            "href": "[link]",
            "h3": "[Title]",
            "p": "[Description]",
            "tier": 2
          },
          {
            "href": "[link]",
            "h3": "[Title]",
            "p": "[Description]",
            "tier": 1
          },
          {
            "href": "[link]",
            "h3": "[Title]",
            "p": "[Description]",
            "tier": 3
          },
          {
            "href": "[link]",
            "h3": "[Title]",
            "p": "[Description]",
            "tier": 1
          },
          {
            "href": "[link]",
            "h3": "[Title]",
            "p": "[Description]",
            "tier": 2
          },
          {
            "href": "[link]",
            "h3": "[Title]",
            "p": "[Description]",
            "tier": 3
          }
        ]
    }
  }
{%for'wiki.resources.items'| translations%}
{{item.p}

{%endfor%}
我显然需要将数组的结果随机化,以某种方式让第一层的项目显示得最多,第二层的项目显示得最多,第三层的项目显示得最少。
|translations
过滤器只会帮助指向带有适当翻译的json文件,以防混淆

到目前为止,我已经尝试从和转换答案,但没有运气

我有一种感觉,我可能无法以最理想的方式翻译Twig的代码,但我正在努力找出答案。任何帮助都将不胜感激

解决了这个问题:

{% set linkWeight = 0 %}
        {% for item in 'wiki.resources.items'|translations %}
            {% set linkWeight = linkWeight + item.weight %}
        {% endfor %}
        {% set randomWeight = random(1,linkWeight) %}
        {% set linkArr = [] %}
        {% for item in 'wiki.resources.items'|translations %}
            {% set randomWeight = randomWeight - item.weight %}
            {% if randomWeight <= 0 %}
               {% set linkArr = linkArr|merge([item]) %}
            {% endif %}
        {% endfor %}
        {% for item in linkArr[:3] %}
            <div class="ressources__item col-12 col-md-4 col-lg-12">
                <a href="{{ item.href }}">
                    <h3 class="ressources__sub">{{ item.h3 }} </h3>
                </a>
                <p>{{ item.p }}</p>
            </div>
        {% endfor %}
{%set linkWeight=0%}
{%用于“wiki.resources.items”| translations%}
{%set linkWeight=linkWeight+item.weight%}
{%endfor%}
{%set randomWeight=random(1,linkWeight)%}
{%set linkArr=[]%}
{%用于“wiki.resources.items”| translations%}
{%set randomWeight=randomWeight-item.weight%}
{%如果随机权重
{% set linkWeight = 0 %}
        {% for item in 'wiki.resources.items'|translations %}
            {% set linkWeight = linkWeight + item.weight %}
        {% endfor %}
        {% set randomWeight = random(1,linkWeight) %}
        {% set linkArr = [] %}
        {% for item in 'wiki.resources.items'|translations %}
            {% set randomWeight = randomWeight - item.weight %}
            {% if randomWeight <= 0 %}
               {% set linkArr = linkArr|merge([item]) %}
            {% endif %}
        {% endfor %}
        {% for item in linkArr[:3] %}
            <div class="ressources__item col-12 col-md-4 col-lg-12">
                <a href="{{ item.href }}">
                    <h3 class="ressources__sub">{{ item.h3 }} </h3>
                </a>
                <p>{{ item.p }}</p>
            </div>
        {% endfor %}