首頁  >  問答  >  主體

如何根據嵌套數組中的列過濾多維數組?

假設我有一個如下所示的陣列:

$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",
                        ], 
    ], 

];

我想過濾這個數組,所以它只包含值不為空的詳細資訊。 Airco 的 type 值為空,因此它不應傳回詳細的 type。在這種情況下,傳回的陣列應如下所示:

$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",
                        ], 
    ], 

];

我知道我可以使用以下程式碼(如此處找到的)基於空列過濾數組:

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

但是由於我有一個巢狀數組details,我不太確定如何調整此程式碼以使其適合我的情況。

P粉004287665P粉004287665431 天前551

全部回覆(1)我來回復

  • P粉458725040

    P粉4587250402023-09-09 09:24:13

    您的array_filter僅在第一層運作。您也希望在 details 陣列上進行循環,您可以使用簡單的 foreach 循環來完成此操作。外部循環將遍歷所有行,內部循環將遍歷每行的詳細資訊

    <?php
    
    foreach($array as &$row){
        foreach($row['details'] as $key => $record){
            if(strlen($record['value']) == 0){
                unset($row['details'][$key]);
            }
        }
    }

    現場示範

    回覆
    0
  • 取消回覆