简体   繁体   English

PHP 用于参考所需元素列表从多维数组中删除不需要的元素

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

Edit: Download sample data here -> $csv编辑:在此处下载示例数据 -> $csv

The array of products at the top level is $csv .顶层的产品数组是$csv In the excerpt below, the numbers [584],[585] represent the next array level down $product => $data_col as you can see below:在下面的摘录中,数字 [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
        )

Thus, I have an array with the structure $csv as $product as $data_col => $value因此,我有一个结构$csv as $product as $data_col => $value的数组

However I want to selectively remove some $data_col arrays with reference to a list of wanted $data_col which I shall refer to as $wanted_data_cols .但是,我想有选择地删除一些$data_col数组,并参考我将其称为$wanted_data_cols的通缉$data_col列表。

What I have tried:我尝试过的:

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

It seems to result in no change to the overall array.它似乎不会导致整个阵列发生变化。 Can you suggest how to make this work, or if you feel you have a superior solution that achieves the same thing, I would accept that.您能否建议如何进行这项工作,或者如果您觉得自己有一个可以实现相同目标的卓越解决方案,我会接受。

The problem with your existing code is that when you try an unset() the value, you are using the actual value rather than the index to remove the item - which won't work.您现有代码的问题在于,当您尝试unset()值时,您使用的是实际值而不是索引来删除项目 - 这将不起作用。

To fix the original code -修复原始代码 -

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

An alternative method is to use array_intersect_key() , this allows you to only leave the keys which are in a second array.另一种方法是使用array_intersect_key() ,这允许您只保留第二个数组中的键。 To do this though - you need to use array_flip() on the fields you want to keep so the field names end up as the keys...但是要做到这一点 - 您需要在要保留的字段上使用array_flip()以便字段名称最终作为键...

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

One last suggestion is that if possible, this should be done when loading the data, this saves holding useless data in memory and removes the need to do a second loop.最后一个建议是,如果可能的话,这应该在加载数据时完成,这样可以将无用的数据保存在内存中,并且不需要进行第二次循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM