PHP 用于根据需要的元素列表从多维数组中删除不需要的元素

PHP for removing unwanted elements from multi-dimensional array with reference to a list of wanted elements

编辑:在此处下载示例数据 -> $csv

顶级产品数组为 $csv。在下面的摘录中,数字 [584]、[585] 代表下一个数组级别 $product => $data_col 如下所示:

 [584] => Array
        (
            [No_] => II14511
            [Manufacturer ID] => CLT-M504S
            [Description] => SAM CON CLT-M504S
            [LQ Price] => 120.00000000000000000000
            [Retail Price Incl_ GST] => 171.60000000000000000000
            [AvailableQty] => 0.00000000000000000000
            [Rocklea] => 0.00000000000000000000
            [Sydney] =>
            [Net Weight] => 0.50000000000000000000
            [Item Category Code] => SAM
            [Product Group Code] => CON
            [Minor Category 1] => TONER
            [Minor Category 2] => #
            [Vendor Name] => Samsung
            [Vendor URL] => www.samsung.com.au
            [Item URL] =>
            [Warranty] =>
            [Dimension] =>
            [EAN Barcode] => 8806085031999
            [Description1] => Samsung CLT-M504S, SEE toner for CLP-415/ CLX-4195 Series LBP & MFP - Magenta Toner 1800 pages<br />
            [Image] => https://auscompcomputers.com/uploads/image/SAM-CON-CLT-M504S.jpg
        )


    [585] => Array
        (
            [No_] => II14772
            [Manufacturer ID] => DK-22205
            [Description] => BRO CON LABELROLL-DK-22205
            [LQ Price] => 25.00000000000000000000
            [Retail Price Incl_ GST] => 35.75000000000000000000
            [AvailableQty] => 0.00000000000000000000
            [Rocklea] => 0.00000000000000000000
            [Sydney] => 0.00000000000000000000
            [Net Weight] => 0.50000000000000000000
            [Item Category Code] => BRO
            [Product Group Code] => CON
            [Minor Category 1] => CON-LABEL-ROLL
            [Minor Category 2] => #
            [Vendor Name] => Brother
            [Vendor URL] => www.brother.com
            [Item URL] =>
            [Warranty] =>
            [Dimension] =>
            [EAN Barcode] => 4977766628198
            [Description1] => Brother DK-22205 ,White Continuous Paper Roll 62mm x 30.48m<br />
            [Image] => https://auscompcomputers.com/uploads/image/BRO-CON-LABELROLL-DK-22205.jpg
        )

因此,我有一个结构为 $csv as $product as $data_col => $value

的数组

但是我想根据需要的 $data_col 列表有选择地删除一些 $data_col 数组,我将其称为 $wanted_data_cols.

我尝试过的:

//The list of $data_col that I wish to keep
$wanted_data_cols = array('Manufacturer ID','LQ Price','AvailableQty','Net Weight','Item Category Code','Product Group Code','Minor Category 1','Vendor Name','Warranty,Dimension','EAN Barcode','Description1','Image');

//If key is not found in $wanted_data_cols, then delete $data_col
foreach ($csv as $product){

        foreach ($product as $data_col){
        if(!in_array($data_col,$wanted_data_cols)){
            unset($csv[$product][$data_col]);
            }
        }
}


print_r($csv);

似乎导致整个数组没有变化。你能建议如何使这项工作有效,或者如果你觉得你有一个更好的解决方案来实现同样的事情,我会接受。

现有代码的问题在于,当您尝试 unset() 值时,您使用的是实际值而不是索引来删除项目 - 这将不起作用。

修复原始代码 -

foreach ($csv as $key => $product){
    foreach ($product as $column => $data_col){
        if(!in_array($column,$wanted_data_cols)){
            unset($csv[$key][$column]);
        }
    }
}

另一种方法是使用array_intersect_key(),这允许您只保留第二个数组中的键。不过,要做到这一点 - 您需要在要保留的字段上使用 array_flip(),以便字段名称最终成为键...

$wanted_data_cols = array_flip($wanted_data_cols);
foreach ($csv as $key => $product){
    $csv[$key] = array_intersect_key($product, $wanted_data_cols);
}

最后一个建议是,如果可能的话,这应该在加载数据时完成,这样可以避免在内存中保留无用数据,并且不需要进行第二次循环。