0

I use the php script below to remove some columns from a csv, order them new and save it as new file. And it works for the file i made it for. Now i need to do the same with another csv but i don't know whats wrong. I always get a comma befor the data in the first column.

This is what i have, but it doesn't really work.

<?php
$input = 'http://***/original.csv';
$output = 'new.csv';

if (false !== ($ih = fopen($input, 'r'))) {
$oh = fopen($output, 'w');

while (false !== ($data = fgetcsv($ih))) {
    // this is where you build your new row
    $outputData = array($data[4], $data[0]);
    fputcsv($oh, $outputData);
}

fclose($ih);
fclose($oh);
}

The original.csv looks that:

subproduct_number   barcode        stock    week_number qty_estimate    productid   variantid
05096470000         4024144513543   J                   3               6           35016
ae214               848518017215    N       23          0               7           35015
05097280000         4024144513727   J                   1               32          34990

The seperator is ';'. The same seperator is used in the file that is working

But here it will be go wrong because my saved new.csv looks like this:

subproduct_number   barcode        stock    week_number qty_estimate    productid   variantid
,05096470000        4024144513543   J                   3               6           35016
,ae214              848518017215    N       23          0               7           35015
,05097280000        4024144513727   J                   1               32          34990

But what i need is a new csv that looks like this:

  qty_estimate  subproduct_number
  3             05096470000
  0             ae214
  1             05097280000

As you can see, i need only the 5. column ($data[4]) as first and the first column ($data[0]) as the second one.

I hope someone can point me in the reight direction. Thanks

4
  • Add echo "<pre/>";print_r($data);die; inside while() and see what output came. show us that output. Commented Dec 6, 2017 at 10:27
  • 1
    If the column separator is not a comma you have to specify it at when calling fgetcsv, Look at the php doc of fgetcsv there is a $delemiter char. Commented Dec 6, 2017 at 10:27
  • I added the echo as suggested from Alive to Die--Anant singh and got this: code Array ( [0] => subproduct_number;barcode;stock;week_number;qty_estimate;productid;variantid; ) (without the first 'code') Commented Dec 6, 2017 at 10:40
  • @ Alive to Die... I did as you suggested below, but now i get only a single column with "qty_estimate,subproduct_number" in the first row. But the column is also empty Commented Dec 6, 2017 at 10:50

1 Answer 1

1

You can do so:

while (false !== ($data = fgetcsv($ih))) {
    $data = explode(';', $data[0]);
    $outputData = array($data[4], $data[0]);
    fputcsv($oh, $outputData, ';');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.