如何按另一个数组中存在的元素过滤我的数组
How can I filter my array by elements that are present in another array
我需要根据另一个数组中存在的元素来过滤我的数组。
更详细地说,我的控制器中有两个变量(数组):一个包含所有用户,另一个包含参与评估的用户。我需要的是第三个 variable/or 树枝(数组)中的列表,它将包含所有其余部分 - 这样我就可以从下拉列表中为每个评估选择它们(已经在评估中的名称不会出现在下拉列表中)。
我现在想知道执行此操作的最佳方法是什么。我应该在树枝中还是在控制器中执行此操作?
谢谢!
树枝:
<select name="user" >
{% for user in users %}
<option value="{{ user.idUser }}" label="{{ user.name }} ">
{% endfor %}
</select>
控制器:
$evals = $this
->getDoctrine()
->getRepository(User::class)
->findUserGroups(); // this is my own function (based on SQL query) from repository that searches for those who participated in evaluation
$users = $this
->getDoctrine()
->getRepository(User::class)
->findAll(); //this is a variable that contains ALL users (including those who already participated in evaluation)
这最好在控制器中处理,您可以使用 php 的 array_diff
来完成。
控制器:
$evals = $this
->getDoctrine()
->getRepository(User::class)
->findUserGroups();
$users = $this
->getDoctrine()
->getRepository(User::class)
->findAll();
$non_evals = array_diff($users, $evals);
然后在树枝中:
<select name="user" >
{% for user in non_evals %}
<option value="{{ user.idUser }}" label="{{ user.name }} ">
{% endfor %}
</select>
我需要根据另一个数组中存在的元素来过滤我的数组。
更详细地说,我的控制器中有两个变量(数组):一个包含所有用户,另一个包含参与评估的用户。我需要的是第三个 variable/or 树枝(数组)中的列表,它将包含所有其余部分 - 这样我就可以从下拉列表中为每个评估选择它们(已经在评估中的名称不会出现在下拉列表中)。
我现在想知道执行此操作的最佳方法是什么。我应该在树枝中还是在控制器中执行此操作?
谢谢!
树枝:
<select name="user" >
{% for user in users %}
<option value="{{ user.idUser }}" label="{{ user.name }} ">
{% endfor %}
</select>
控制器:
$evals = $this
->getDoctrine()
->getRepository(User::class)
->findUserGroups(); // this is my own function (based on SQL query) from repository that searches for those who participated in evaluation
$users = $this
->getDoctrine()
->getRepository(User::class)
->findAll(); //this is a variable that contains ALL users (including those who already participated in evaluation)
这最好在控制器中处理,您可以使用 php 的 array_diff
来完成。
控制器:
$evals = $this
->getDoctrine()
->getRepository(User::class)
->findUserGroups();
$users = $this
->getDoctrine()
->getRepository(User::class)
->findAll();
$non_evals = array_diff($users, $evals);
然后在树枝中:
<select name="user" >
{% for user in non_evals %}
<option value="{{ user.idUser }}" label="{{ user.name }} ">
{% endfor %}
</select>