使用多个过滤器过滤Array of Array

时间:2019-11-07 10:36:13

标签: php arrays filter

我不知道是否提出了这个问题,但是我找不到答案。 我有一个数组数组,我需要组合过滤器才能查看结果。 从选择中,我选择了能够过滤结果的数据。 我需要显示所有包含两个过滤器的结果。

<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)

我的目标是能够同时打印与两个过滤器相对应的结果。

对不起,我的英语。

谢谢

数组

Array ( 
    [1] => Array ( 
        [0] => (apple) 
        [1] => (not set) 
        [2] => 20191001 
        [3] => 123456 
        [4] => cereal
    ) 
    [2] => Array ( 
        [0] => (apple) 
        [1] => (not set) 
        [2] => 20191001 
        [3] => 7894561 
        [4] => cacao
    ) 
    [3] => Array ( 
        [0] => (melon) 
        [1] => (not set)
        [2] => 20191002 
        [3] => 123489 
        [4] => null
    ) 
    [4] => Array ( 
        [0] => (pear) 
        [1] => (not set) 
        [2] => 20191002 
        [3] => 365478 
        [4] => cacao
    ) 
    [5] => Array ( 
        [0] => (pear) 
        [1] => (not set) 
        [2] => 20191002 
        [3] => 1697823 
        [4] => banana
    ) 
    [6] => Array ( 
        [0] => (strawberry) 
        [1] => (not set) 
        [2] => 20191002 
        [3] => 6578931 
        [4] => null
    )

1 个答案:

答案 0 :(得分:-1)

我将通过在两个过滤器中创建一个数组来解决它,并使用'in_array'检查该值 像这样:

$filters = [$filter1,$filter2];
        foreach ($complete as $index => $subdime) {
            if(in_array($subdime[0],$filters)){
                // value matches one of the filters - do something
            }
        }