我有一个数组的数组,我需要实现过滤器的组合才能达到预期的结果。
从选择的字段中,我获取数据以便能够过滤结果。
我需要显示包含两个过滤器的所有结果。
<select name="filter">
<option value="1">apple</option>
<option value="2">pear</option>
</select>
<label>Combine</label>
<select name="filter2">
<option value="1">melon</option>
<option value="2">strawberry</option>
</select>
foreach ($complete as $subdime) {
$substr = $filter;
$substr2 = $filter2;
if (
$filter !== 'total'
&& strpos($subdime[0], $substr) !== false
&& $filter2 === ""
) {
/ * print result */
}
}
只需一个过滤器,一切正常。 如果我插入一个 AND 来可视化两者,则向量为空。
if (
$filter !== 'total'
&& strpos($subdime[0], $substr) !== false
&& strpos($subdime[0], $substr2) !== false
) {
我的目标是能够同时打印与两个过滤器相对应的结果。
$complete
数组:
[
1 => [
'(apple)'
'(not set)',
20191001,
123456,
'cereal'
],
2 => [
'(apple)',
'(not set)',
20191001,
7894561,
'cacao'
],
3 => [
'(melon)',
'(not set)',
20191002,
123489,
null
],
4 => [
'(pear)',
'(not set)',
20191002,
365478,
'cacao'
],
5 => [
'(pear)',
'(not set)',
20191002,
1697823,
'banana'
],
6 => [
'(strawberry)',
'(not set)',
20191002,
6578931,
null
]
我会通过在两个过滤器中创建一个数组并使用“in_array”检查值来解决这个问题 像这样:
$filters = [$filter1,$filter2];
foreach ($complete as $index => $subdime) {
if(in_array($subdime[0],$filters)){
// value matches one of the filters - do something
}
}