与Twig相交两个数组 [英] Intersect two arrays with Twig
本文介绍了与Twig相交两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个要交集的数组.我得到了这两个数组:
I have two arrays which i'd like to intersect. I got these two arrays:
{{dump(array1)}}
{{ dump(array1) }}
array(6) {
[0]=>
array(2) {
["id"]=>
int(121)
["text"]=>
string(3) "uno"
}
[1]=>
array(2) {
["id"]=>
int(122)
["text"]=>
string(3) "dos"
}
[2]=>
array(2) {
["id"]=>
int(123)
["text"]=>
string(4) "tres"
}
[3]=>
array(2) {
["id"]=>
int(124)
["text"]=>
string(6) "cuatro"
}
[4]=>
array(2) {
["id"]=>
int(125)
["text"]=>
string(5) "cinco"
}
[5]=>
array(2) {
["id"]=>
int(126)
["text"]=>
string(4) "seis"
}
}
{{dump(array2)}}
{{ dump(array2) }}
array(3) {
[0]=>
array(2) {
["id"]=>
int(124)
["text"]=>
string(6) "cuatro"
}
[1]=>
array(2) {
["id"]=>
int(125)
["text"]=>
string(5) "cinco"
}
[2]=>
array(2) {
["id"]=>
int(126)
["text"]=>
string(4) "seis"
}
[3]=>
array(2) {
["id"]=>
int(127)
["text"]=>
string(5) "siete"
}
}
我希望我的第三个数组具有这样的结果:
And i want my third array to have a result like this:
array(3) {
[0]=>
array(2) {
["id"]=>
int(124)
["text"]=>
string(6) "cuatro"
}
[1]=>
array(2) {
["id"]=>
int(125)
["text"]=>
string(5) "cinco"
}
[2]=>
array(2) {
["id"]=>
int(126)
["text"]=>
string(4) "seis"
}
}
问题是:如何在树枝中使用简单的滤镜将它们相交?
The question is: How do I intersect them using a simple filter in twig?
推荐答案
更好的解决方案是在执行输出之前,将它们在我的控制器或服务中与php相交:
Better solution was to intersect them in my controller or service with php before doing an output:
array_intersect($array1, $array2);
Twig打算在模板中显示数据,而无需像@sjagr在其评论中建议的那样进行过多处理.
Twig is intended to display data in a template without doing too much processing as @sjagr suggested in his comment.
这篇关于与Twig相交两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文