Search code examples
phpmultidimensional-arrayunset

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


Edit: Download sample data here -> $csv

The array of products at the top level is $csv. In the excerpt below, the numbers [584],[585] represent the next array level down $product => $data_col as you can see below:

 [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

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.

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.


Solution

  • 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.

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

    $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.