How to filter multidimensional array based on columns in nested array?
P粉004287665
P粉004287665 2023-09-08 13:16:28
0
1
357

Suppose I have an array like this:

$array =    [    

    0 => [  
        "label"    =>  "Radiator",  
        "details"  =>   0 => [  
                            "label"     => "Condition",  
                            "value"     => "New",
                        ], 
                        1 => [  
                            "label"     => "Type",  
                            "value"     => "Wall",
                        ],
    ],  
    1 => [  
        "label"    =>  "Airco",  
        "details"  =>   0 => [  
                            "label"     => "Condition",  
                            "value"     => "New",
                        ], 
                        1 => [  
                            "label"     => "Type",  
                            "value"     => "",
                        ], 
    ], 
    2 => [  
        "label"    =>  "Refrigerator",  
        "details"  =>   0 => [  
                            "label"     => "Condition",  
                            "value"     => "Bad",
                        ], 
                        1 => [  
                            "label"     => "Type",  
                            "value"     => "Wall",
                        ], 
    ], 

];

I want to filter this array so it only contains details where the value is not empty. Airco's type value is empty, so it should not return detailed type. In this case, the returned array should look like this:

$array =    [    

    0 => [  
        "label"    =>  "Radiator",  
        "details"  =>   0 => [  
                            "label"     => "Condition",  
                            "value"     => "New",
                        ], 
                        1 => [  
                            "label"     => "Type",  
                            "value"     => "Wall",
                        ],
    ],  
    1 => [  
        "label"    =>  "Airco",  
        "details"  =>   0 => [  
                            "label"     => "Condition",  
                            "value"     => "New",
                        ],
    ], 
    2 => [  
        "label"    =>  "Refrigerator",  
        "details"  =>   0 => [  
                            "label"     => "Condition",  
                            "value"     => "Bad",
                        ], 
                        1 => [  
                            "label"     => "Type",  
                            "value"     => "Wall",
                        ], 
    ], 

];

I know I can filter an array based on empty columns using the following code (as found here):

$result = array_filter($array, function($o) use($column) {
    return trim( $o[$column] ) !== '' && $o[$column] !== null;
});

But since I have a nested arraydetails, I'm not quite sure how to adjust this code to make it work for my case.

P粉004287665
P粉004287665

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!