PHP CSV Datei schreiben

Gast #3782284
Lesenswert?

Hallo zusammen,
ich stehe leicht auf dem schlauch irgendwo in der foreach schleife bei 
writeCSV ist ein Problem, vllt. kann mich jmd vom schlauch runterholen:

Am Ende soll das File benzin.cv so aussehen:
1
Preis,Altlast
2
0,0
3
0,0

Hier der Code
1
 <?php
2

3
 $GodArray=array(
4
          1 => array(
5
                'Preis'=>0,
6
                'Altlast'=>0
7
                ),
8
          2 => array(
9
                'Preis'=>0,
10
                'Altlast'=>0
11
                )
12
        )  ;
13
 
14

15
/**
16
* Set a named configuration template from which a cache object can later
17
*
18
* @param arrayarray $CSVElements
19
* @param string $filename
20
* @param bool $headline
21
* @param bool $overwrite
22
* @return integer returncode
23
*/
24
function writeCSVFile($CSVElements, $filename, $headline, $overwrite)
25
{
26
  $returncode = 1;
27
  
28
  if ($overwrite)
29
  {
30
    $fp = fopen($filename, 'w');
31
  }
32
  else
33
  {
34
    $fp = fopen($filename, 'a');
35
  }
36
  
37
  (!$fp) ? $returncode = 1 : $returncode = 0;
38
  
39
  if ($headline && $returncode == 0)
40
  {
41
    try
42
    {
43
      foreach ($CSVElements as $number => $properties) 
44
      {
45
        foreach ($properties as $property => $value) 
46
        {
47
          fputcsv($fp, $property);
48
        }
49
        break;
50
      }
51
    }
52
    catch (Exception $e)
53
    {
54
      $returncode = 3;
55
    }
56
  }
57
  else
58
  {
59
    $returncode = 2;
60
  }
61
  
62
  
63
  if (  $returncode == 0 )
64
  {
65
    try
66
    {
67
      foreach ($CSVElements as $number => $properties) 
68
      {
69
        foreach ($properties as $property => $value) 
70
        {
71
          fputcsv($fp, $value);
72
        }
73
      }
74
    }
75
    catch (Exception $e)
76
    {
77
      $returncode = 5;
78
    }
79
  }
80
  else
81
  {
82
    $returncode = 4;
83
  }
84
  try
85
  {
86
    fclose($fp);
87
  }
88
  catch (Exception $e)
89
  {
90
    $returncode = 6;
91
  }
92
  
93
  return $returncode;
94
}
95

96

97

98

99
/**
100
* Set a named configuration template from which a cache object can later
101
*
102
* @param arrayarray $CSVElements
103
* @param string $filename
104
* @return integer returncode
105
*/
106
function readCSVFile(&$CSVElements, $filename,  $headline)
107
{
108
  $file_handle = fopen($filename, "r");
109
  $headline_read = true;
110
  
111
  $i = 0;
112
  while (!feof($file_handle) ) 
113
  {
114
    $line = fgetcsv($file_handle, 1024);
115
    
116
    if ( $headline )
117
    {
118
      if ( $headline_read )
119
      {
120
        for ($j = 0; $j < count($line) ; $j++)
121
        {
122
          $headline_elements[$j] = $line[$j];
123
        }
124
        $headline_read = false;
125
      }
126
      else
127
      {
128
        for ($j = 0; $j < count($line) ; $j++)
129
        {
130
          $CSVElements[$i][$headline_elements[$j]] = $line[$j];
131
        }
132
        $i++;
133
      
134
      }
135
    }
136
    else
137
    {
138
        for ($j = 0; $j < count($line) ; $j++)
139
        {
140
          $CSVElements[$i][$j] = $line[$j];
141
        }
142
        $i++;
143
    }
144
  }
145

146
  fclose($file_handle);
147
}
148
  
149
writeCSVFile($GodArray, 'benzin.csv', true, true);
150
readCSVFile($GodArray1, 'benzin.csv', true);
151
      foreach ($GodArray1 as $number => $properties) 
152
      {
153
        
154
        foreach ($properties as $property => $value) 
155
        {
156
          echo $number.$property.$value;
157

158
        }
159
      }
160

161
?>

Danke
#3782570
Lesenswert?

edit fragt:
woher hast du denn die read und write-funktionen?

---

und wie sieht das file im moment aus?

egal
1
printHeadline();
2

3
foreach($GodArray as $entry) {
4
  implode(",", $entry);
5
}

nein, das ist nicht zu 100% das was gefragt war - aber es sind ein paar 
tipps wie das ganze schneller und einfacher gehen könnte

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren