PHP在多维数组上应用过滤器

时间:2017-12-11 19:37:23

标签: php arrays

我有这个数组:

Array
(
[54190] => Array
    (
        [Dealer] => RETIRO ANDEN ANDEN
        [Products] => Array
            (
                [SWEETS] => Array
                    (
                        [2000 - 0] => Array
                            (
                                [DESC] => KIT KAT
                                [DIM] => 36X41.5G
                            )
                        [3993 - 0] => Array
                            (
                                [DESC] => COOKIE COCO
                                [DIM] => 30 X 120 GRM
                            )
                    )
                [REFRIGERATED] => Array
                    (
                        [1000 - 0] => Array
                            (
                                [DESC] => SEM C/CCHA
                                [DIM] => 9X4X135
                            )
                    )
            )
    )
[54183] => Array
    (
        [Dealer] => MIGUEL PEREZ (HIJO)
        [Products] => Array
            (
                [TRADITIONAL] => Array
                    (
                        [200 - 0] => Array
                            (
                                [DESC] => COFFEE STICK CL
                                [DIM] => 77X172.8G
                            )
                    )
            )
    )
[54451] => Array
    (
        [Dealer] => HUGO FRUGO.
        [Products] => Array
            (
                [SWEETS] => Array
                    (
                        [3993 - 0] => Array
                            (
                                [DESC] => COOKIE COCO
                                [DIM] => 30 X 120 GRM
                            )
                    )
            )
    )
[54107] => Array
    (
        [Dealer] => JAVIER M.
        [Products] => Array
            (
                [SWEETS] => Array
                    (
                        [2001 - 0] => Array
                            (
                                [DESC] => COOKIE VAINILLA
                                [DIM] => 30 X 120 GRM
                            )
                    )
            )
    )
)

我需要应用一个过滤器来显示那些包含在[Products]数组中的数组,以下代码' 2000 - 0'或者' 2001 - 0'并且还排除了其他产品。

输出:

Array
(
[54190] => Array
    (
        [Dealer] => RETIRO ANDEN ANDEN
        [Products] => Array
            (
                [SWEETS] => Array
                    (
                        [2000 - 0] => Array
                            (
                                [DESC] => KIT KAT
                                [DIM] => 36X41.5G
                            )
                    )
            )
    )
[54107] => Array
    (
        [Dealer] => JAVIER M.
        [Products] => Array
            (
                [SWEETS] => Array
                    (
                        [2001 - 0] => Array
                            (
                                [DESC] => COOKIE VAINILLA
                                [DIM] => 30 X 120 GRM
                            )
                    )
            )
    )
)

我解决了这个问题,答案如下。并且完美地适用于此案。

特别感谢隐身用户帮助,但消除了他的回应。 :)

1 个答案:

答案 0 :(得分:0)

解决方案:

$find = array('2000 - 0', '2001 - 0');
    foreach ($ArrayIn as $k1 => $v1) {
        foreach ($v1['Products'] as $v2 => $v3) {
            foreach ($v3 as $key => $value) {
                    if(in_array($key, $find)){

                        if($Last != $k1){
                        $Line = array();
                        $Last=$k1;
                        }

                        $Line[$v2][$key]= $ArrayIn[$k1]['Products'][$v2][$key];

                        $output[$k1] = $output[$k1] = array(
                                        'Dealer' => $ArrayIn[$k1]['Dealer'],
                                        'Products' => $Line
                                        );
                    }
                }
            }
        }