0

I read most of the discussions about my problem but I do not find a solution. So, I have a .csv file from which I read, extract all the content and populate a multidimensional array. That is the code I wrote to do that:

 if (($handle = fopen("dateRegion.csv", "r")) !== FALSE) {
 # Set the parent multidimensional array key to 0.
 $pr = 0;
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    # Count the total keys in the row.
    $count = count($data);
    # Populate the multidimensional array.
    for ($x = 0; $x < $count; $x++) {
        $brim[$pr][$x] = $data[$x];
    }
 $pr++;
  }
fclose($handle);

}

After that, I extract the element I need and put it into another array. That array will be the content of the new .csv file.

This is the code:

      $parserCsv = array();
      $dip=1;
       do {
        $region = $brim[$dip][7];
        $sex = $brim[$dip][8];
        $frequency = $brim[$dip][9];
        $value = $brim[$dip][10];
        $parserCsv[] = array($region, $sex, $frequency, $value);
      $dip++;
       } while($dip <= 6336);

This is the "var_dump()" of the array:

Array(
[0] => Piemonte
[1] => maschi
[2] => 2005
[3] => 16.972) 

etc.

I tried to put the content of the array $parserCsv using the method fputcsv(), with that script:

$fp = fopen('csvExtract.csv', 'w');
 foreach ($parserCsv as $fields) {
  fputcsv($fp, $fields);
 }
fclose($fp);

but it did not work. The content of the file csvExtract.csv is blank. I do not understand my mistake, I tried other solution like create the array $parserCsv like:

 $parserCsv = array(
    array($region), array($sex), array($frequency), array($value));

and nothing change. Does anyone have some advise?

EDIT: Edited the code with the solution suggested by mkjasinski! The code is working now. Thanks for all the replies.

Brus

2
  • Do you have write access to the directory? Commented Mar 29, 2013 at 13:38
  • Yes i have. I'm the owner of the server where the scripts are running. Commented Mar 29, 2013 at 13:40

2 Answers 2

1

Try this:

if (($handle = fopen("dateRegion.csv", "r")) !== FALSE) {
    # Set the parent multidimensional array key to 0.
    $pr = 0;
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        # Count the total keys in the row.
        $count = count($data);
        # Populate the multidimensional array.
        for ($x = 0; $x < $count; $x++) {
            $brim[$pr][$x] = $data[$x];
        }
        $pr++;
    }
    fclose($handle);
}

and this:

$parserCsv = array();
$dip=1;
do {
    $region = $brim[$dip][7];
    $sex = $brim[$dip][8];
    $frequency = $brim[$dip][9];
    $value = $brim[$dip][10];
    $parserCsv[] = array($region, $sex, $frequency, $value);
    $dip++;
} while($dip <= 6336);

and save:

$fp = fopen('csvExtract.csv', 'w');
foreach ($parserCsv as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);
Sign up to request clarification or add additional context in comments.

Comments

0

are you getting some errors? Is the file open in some other software? (like MS Excel?)

Following code enters 4 records in CSV

$parserCsv = array(
   array('Piemonte'),
   array('maschi'),
   array(2005),
   array(16.972)
);

$fp = fopen('blah.csv', 'w');
foreach($parserCsv as $fields)
{
   fputcsv($fp, $fields);
}
fclose($fp);

3 Comments

Hi Ejay, thanks for the reply. No errors in the code you posted. I already tryed that way, and it works. I'm not understanding why if i give the array $parserCsv (with only the elements i need) to the fputcsv method, it doesn't work properly. Any Idea?
fput_csv seconds argument to the function has got to be an array, you provide String in your first example.
extending the discussion to make it clear (for future reader) foreach ($parserCsv as $fields) { inside foreach block, $fields is a String (in your original code)

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.