-3

I have this array:

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

And I need to apply a filter to show only those arrays that contain within the [Products] array the following codes '2000 - 0' or '2001 - 0' and also excluding the rest of the products.

Output:

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

And i solved the question, the answer is below. and works perfectly to the case.

Special thanks to incognito user that helped, but eliminated his response. :)

Marcelo
  • 37
  • 1
  • 7
  • http://php.net/manual/en/function.array-filter.php – Sammitch Dec 11 '17 at 19:45
  • Hint: For each dealer filter the products down to what you are looking for, then filter out the dealers with no more products. – Sammitch Dec 11 '17 at 20:05
  • This will help, http://php.net/manual/en/function.array-key-exists.php if you have any problem we will help you. – Mohammed Alhanafi Dec 11 '17 at 20:09
  • Possible duplicate of [Check if specific array key exists in multidimensional array - PHP](https://stackoverflow.com/questions/19420715/check-if-specific-array-key-exists-in-multidimensional-array-php) – splash58 Dec 11 '17 at 22:32

1 Answers1

0

Solution:

$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
                                        );
                    }
                }
            }
        }
Marcelo
  • 37
  • 1
  • 7