如何在 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 上删除数组中的重复项?
我在树枝中有数组值,例如。
{{ 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 %}
In this case, as @Webberig said it's better to prepare your data before the rendering of the view. But when you have a more complex process and if is related to the view you can create a Twig Extension, with an extension the code would look like:
MyTwigExtension.php
for Twig versions 1.12 and greater:
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('array_unset', array($this, 'arrayUnset'))
);
}
If you are on a Twig version earlier than 1.12, use this MyTwigExtension.php
instead:
/**
* {@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;
}
Twig:
{% 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
{%- set mylist = ['alpha','alpha','bravo','charlie','alpha','alpha','delta','echo'] -%} 前: ['alpha','alpha','bravo','charlie','alpha','alpha','delta','echo'] 后: ['alpha','bravo','charlie','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 -%}
对于 symfony !
服务: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')];
}
}
谢谢: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 和 title 的对象。
in
操作员进行遏制测试。
如果左操作数包含在右操作数中,则返回 true。
{% if name in array %}
http://twig.sensiolabs.org/doc/templates.html#containment-operator