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.