回答編集履歴
3
ストリームフィルタ
test
CHANGED
@@ -173,3 +173,45 @@
|
|
173
173
|
```
|
174
174
|
|
175
175
|
|
176
|
+
|
177
|
+
---
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
そもそも、今の御時世なら tmpfile なんて使うよりもストリームフィルタで良いと思われます。
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
```lang-php
|
186
|
+
|
187
|
+
<?php
|
188
|
+
|
189
|
+
$file = '1-1.csv';
|
190
|
+
|
191
|
+
$fp = fopen($file, 'r');
|
192
|
+
|
193
|
+
$sh = stream_filter_prepend($fp, 'convert.iconv.cp932/utf-8', STREAM_FILTER_READ);
|
194
|
+
|
195
|
+
$csv = array();
|
196
|
+
|
197
|
+
$keys = null;
|
198
|
+
|
199
|
+
while ($data = fgetcsv($fp, 1000,",")) {
|
200
|
+
|
201
|
+
if ($keys === null) {
|
202
|
+
|
203
|
+
$keys = $data;
|
204
|
+
|
205
|
+
} else {
|
206
|
+
|
207
|
+
$csv[] = array_combine($keys, $data);
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
print_r($csv);
|
214
|
+
|
215
|
+
```
|
216
|
+
|
217
|
+
|
2
追記
test
CHANGED
@@ -84,4 +84,92 @@
|
|
84
84
|
|
85
85
|
|
86
86
|
|
87
|
+
---
|
87
88
|
|
89
|
+
|
90
|
+
|
91
|
+
もしくは、次のように CSV からの読み込みのループの時点で連想配列にしてしまうこともできます。
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
```lang-php
|
96
|
+
|
97
|
+
<?php
|
98
|
+
|
99
|
+
$file = '1-1.csv';
|
100
|
+
|
101
|
+
$data = file_get_contents($file);
|
102
|
+
|
103
|
+
$data = mb_convert_encoding($data, 'UTF-8','sjis-win');
|
104
|
+
|
105
|
+
$temp = tmpfile();
|
106
|
+
|
107
|
+
$csv = array();
|
108
|
+
|
109
|
+
fwrite($temp,$data);
|
110
|
+
|
111
|
+
rewind($temp);
|
112
|
+
|
113
|
+
$keys = null;
|
114
|
+
|
115
|
+
while ($data = fgetcsv($temp, 1000,",")) {
|
116
|
+
|
117
|
+
if ($keys === null) {
|
118
|
+
|
119
|
+
$keys = $data;
|
120
|
+
|
121
|
+
} else {
|
122
|
+
|
123
|
+
$csv[] = array_combine($keys, $data);
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
print_r($csv);
|
130
|
+
|
131
|
+
/*
|
132
|
+
|
133
|
+
Array
|
134
|
+
|
135
|
+
(
|
136
|
+
|
137
|
+
[0] => Array
|
138
|
+
|
139
|
+
(
|
140
|
+
|
141
|
+
[code] => A10
|
142
|
+
|
143
|
+
[name] => 太田店
|
144
|
+
|
145
|
+
[place] => 新宿
|
146
|
+
|
147
|
+
[] =>
|
148
|
+
|
149
|
+
)
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
[1] => Array
|
154
|
+
|
155
|
+
(
|
156
|
+
|
157
|
+
[code] => A11
|
158
|
+
|
159
|
+
[name] => 山田店
|
160
|
+
|
161
|
+
[place] => 五反田
|
162
|
+
|
163
|
+
[] =>
|
164
|
+
|
165
|
+
)
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
)
|
170
|
+
|
171
|
+
*/
|
172
|
+
|
173
|
+
```
|
174
|
+
|
175
|
+
|
1
空の除去
test
CHANGED
@@ -63,3 +63,25 @@
|
|
63
63
|
*/
|
64
64
|
|
65
65
|
```
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
`[] =>` のような空の部分が邪魔なら、↑の結果をさらに次のようにすれば除去できます。
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
```lang-php
|
74
|
+
|
75
|
+
$ret = array_map(function ($row) {
|
76
|
+
|
77
|
+
unset($row['']);
|
78
|
+
|
79
|
+
return $row;
|
80
|
+
|
81
|
+
}, $ret);
|
82
|
+
|
83
|
+
```
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|