Home > front end >  multi PHP arrays with same keys and different values to CSV file and special characters
multi PHP arrays with same keys and different values to CSV file and special characters

Time:10-01

I have many arrays that share the same keys but in different values, and different order:

$array = array(
          'English' => array(
                             '1' => 'Apples',
                             '2' => 'Oranges',
                             '3' => 'Bananas'
                            ),
           'Spanish' => array(
                             '3' => 'Plátanos',
                             '1' => 'Manzanas',
                             '2' => 'Naranjas'
                            ),
            'German' => array(
                             '1' => 'Äpfel',
                             '3' => 'Bananen',
                             '2' => 'Orangen'
                            ),
              );

and I want to make a CSV file out of the arrays expecting each array values to be listed in a column next to the value from other arrays that have the same key like in the image:

I tried this:

 $header = array(
        'Key', 'English', 'Spanish', 'German'
    );

  $f_name = 'lang.csv';
  header("Content-type: text/csv");
  header("Content-Disposition: attachment; filename=$f_name");
  $output = fopen("php://output" , "w");
  fputcsv($output, $header);
  
  foreach($array as $row){
      fputcsv($output , array_values( $row ));
  }
  fclose($output);

but the results are like in the image below:

CodePudding user response:

Since you have your inner arrays out of order the obvious thing is to sort by the numeric key. Try ksort in your loop:

...
ksort($row);
fputcsv($output , array_values( $row ));
...

CodePudding user response:

Your code tries to fputcsv of a $row which holds only 3 values and you want 4 values a key and the others...

First thing first: use UTF-8 for these character encoding and your text should be fine -- meaning your php file encoding should be UTF-8.

As nitrin0 pointed out a good thing would be to ksort($row); but you want to add a fourth value to the $row which holds only 3 values , we add the key even with a $c also the array_values is redundant here.

Example:

<?php

$array = array(
          'English' => array(
                             '1' => 'Apples',
                             '2' => 'Oranges',
                             '3' => 'Bananas'
                            ),
           'Spanish' => array(
                             '3' => 'Plátanos',
                             '1' => 'Manzanas',
                             '2' => 'Naranjas'
                            ),
            'German' => array(
                             '1' => 'Äpfel',
                             '3' => 'Bananen',
                             '2' => 'Orangen'
                            ),
              );

$header = array(
    'Key', 'English', 'Spanish', 'German'
);

$output = fopen("php://output" , "w");
fputcsv($output, $header);
$c = 1;
foreach($array as $row){
    $row[0] = $c;
    ksort($row);
      $c;
    fputcsv($output , $row);
}
fclose($output);

will output:

Key,English,Spanish,German
1,Apples,Oranges,Bananas
2,Manzanas,Naranjas,Plátanos
3,Äpfel,Orangen,Bananen

This solution relies heavily on the fact that in your $array array you had no 0 keys for the fruits...

so instead you can work without relying on 0 keys not existing and stuff without ksort but with merge:

$c = 1; 
foreach($array as $row){
    $arr[] = $c;
    $arr = array_merge($arr,$row);
    //print_r($arr);
    //ksort($row);
      $c;
    //print_r($row);
    fputcsv($output , $arr);
    $arr = [];
}
fclose($output);
echo '<pre>';
print_r($output);

Will output the same output as the other code example.

  • Related