如何删除 Twig 上数组中的重复项?
我在树枝中有数组值,例如。
{{ set array = ["testA","testB","testA","testC","testB"] }}
我想删除重复的项目并仅使用testA,testB,testC
{% for name in array%}
//skip the duplicate items and use only testA,testB,testC
{% endfor %}
我怎样才能做到?
Twig 是一个 VIEW 引擎,理论上不应该用来操作数据。使用(假设)PHP 收集数据、执行所有必要的操作然后将正确的数据传递到您的视图是一个(非常)好的做法。
也就是说,您可以使用纯 Twig 语法来完成此操作:
{% set newArray = [] %}
{% for name in array %}
{% if name not in newArray %}
My name is {{name}}
{% set newArray = newArray|merge([name]) %}
{% endif %}
{% endfor %}
在这种情况下,正如@Webberig所说,最好在渲染视图之前准备好数据。但是,当您有更复杂的流程并且与视图相关时,您可以创建一个 Twig 扩展,带有扩展的代码将如下所示:
MyTwigExtension.php
适用于 Twig 版本 1.12 及更高版本:
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('array_unset', array($this, 'arrayUnset'))
);
}
如果您使用的是 1.12 之前的 Twig 版本,请使用此
MyTwigExtension.php
代替:
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
'array_unset' => new \Twig_Function_Method($this, 'arrayUnset')
);
}
/**
* Delete a key of an array
*
* @param array $array Source array
* @param string $key The key to remove
*
* @return array
*/
public function arrayUnset($array, $key)
{
unset($array[$key]);
return $array;
}
树枝:
{% set query = array_unset(query, 'array_key') %}
从 Twig 1.41 和 2.10 开始,你可以用一行代码完成:
{% set unique = array|reduce(
(unique, item) => item in unique ? unique : unique|merge([item]), []
) %}
在你的例子中:
{% for name in array|reduce((unique, item) => item in unique ? unique : unique|merge([item]), []) %}
My name is {{name}}
{% endfor %}
array_unique
允许删除重复项BEFORE
转变为 AFTER
{%- 设置 mylist = ['alpha','alpha','bravo','charlie','alpha','alpha','delta','echo'] -%} 前: ['阿尔法','阿尔法','布拉沃','查理','阿尔法','阿尔法','德尔塔','回声'] 后: ['阿尔法','布拉沃','查理','德尔塔','回声']
[...] // {"define_filter_":"array_unique" ,"desc":"PHP array_unique 函数" }, $filter = new Twig_SimpleFilter('array_unique', function ( $vinp=Array() ) { $vout = ( $vinp ); $vout = array_unique( $vout ); $vout = array_values( $vout ); // PHP 烦恼数组重新索引 -- 可选 返回$vout; }); $twig->addFilter($filter); //;; [...]输出
{%- 设置 mylist = mylist|array_unique -%}
为了交响乐!
服务:app/config/service.yml
twig_array_unique_function:
class: AppBundle\Twig\ArrayUniqueFunction
tags:
- { name: twig.function }
类:src/AppBundle/Twig/ArrayUniqueFunction.php
<?php
namespace AppBundle\Twig;
class ArrayUniqueFunction extends \Twig_Extension
{
public function getFilters()
{
return [new \Twig_SimpleFilter('array_unique', 'array_unique')];
}
public function getFunctions()
{
return [new \Twig_SimpleFunction('array_unique', 'array_unique')];
}
}
Thk:https://github.com/getgrav/grav/blob/develop/system/src/Grav/Common/Twig/TwigExtension.php
{% set keeper = {} %}
{% set throwaway = [] %}
{% for thing in yourSet.all() %}
{% if thing.id not in throwaway %}
{% set throwaway = throwaway|merge([thing.id]) %}
{% set keeper = keeper|merge([{ slug: thing.slug, title: thing.title}]) %}
{% endif %}
{% endfor %}
对上面接受的答案进行修改。 我需要从集合中删除重复项,最终得到一个只有 slug 和标题的对象。
in
操作员执行遏制测试。
如果左操作数包含在右操作数中,则返回 true。
{% if name in array %}
http://twig.sensiolabs.org/doc/templates.html#containment-operator