File: reading.md

package info (click to toggle)
php-league-csv 9.23.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 39,720 kB
  • sloc: php: 13,345; javascript: 80; makefile: 29; xml: 29
file content (405 lines) | stat: -rw-r--r-- 10,678 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
---
layout: default
title: Extracting data from a CSV
redirect_from: /reading/
---

# Extracting data

To extract data from a CSV document use `League\Csv\Reader` methods.

<p class="message-warning">
By default, the mode for a <code>Reader::createFromPath</code> is
<code>r+</code> which looks for write permissions on the file and throws an <code>Exception</code> if
the file cannot be opened with the permission set. For sake of clarity, it is
strongly suggested to set <code>r</code> mode on the file to ensure it can be opened.</p>

## Reader::fetch

The `fetch` method fetches the next row from the `Iterator` result set.

```php
public Reader::fetch(callable $callable = null): Iterator
```

The method takes an optional callable parameter to apply to each row of the resultset before returning. The callable signature is as follow:

```php
function(array $row [, int $rowOffset [, Iterator $iterator]]): array
```

- `$row`: the CSV current row as an array
- `$rowOffset`: the CSV current row offset
- `$iterator`: the current CSV iterator

### Example 1

```php
use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$results = $reader->fetch();
foreach ($results as $row) {
    //do something here
}
```

### Example 2 - with a callable

```php
use League\Csv\Reader;

$func = function ($row) {
    return array_map('strtoupper', $row);
};

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$results = $reader->fetch($func);
foreach ($results as $row) {
    //each row member will be uppercased
}
```

## Reader::fetchAll

`fetchAll` returns a sequential `array` of all rows.

```php
public Reader::fetchAll(callable $callable = null): array
```

`fetchAll` behaves exactly like `fetch` with one difference:

- `fetchAll` returns an `array`.

## Reader::fetchOne

`fetchOne` return one single row from the CSV data as an `array`.

```php
public Reader::fetchOne($offset = 0): array
```

The required argument `$offset` represents the row index starting at `0`. If no argument is given the method will return the first row from the CSV data.

### Example

```php
use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$data = $reader->fetchOne(3); ///accessing the 4th row (indexing starts at 0)
// will return something like this :
//
//   ['john', 'doe', 'john.doe@example.com']
//
```

## Reader::each

`each` applies a callable function on each CSV row.

```php
public Reader::each(callable $callable): int
```

The method returns the number of successful iterations.

The callable signature is as follows:

```php
function(array $row [, int $rowOffset [, Iterator $iterator]]): bool
```

- `$row`: the CSV current row as an array
- `$rowOffset`: the CSV current row offset
- `$iterator`: the current CSV iterator

The callable must return `true` to continue iterating over the CSV;

### Example - Counting the CSV total number of rows

```php
use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');

//count the numbers of rows in a CSV
$nbRows = $reader->each(function ($row) {
    return true;
});
```

## Reader::fetchAssoc

<p class="message-warning"><strong>BC Break:</strong> Starting with version <code>8.0.0</code> This method returns an <code>Iterator</code>.</p>

`fetchAssoc` returns an `Iterator` of all rows. The rows themselves are associative arrays where the keys are a one dimension array. This array must only contain unique `string` and/or `scalar` values.

```php
public Reader::fetchAssoc(
    mixed $offset_or_keys = 0,
    callable $callable = null
): Iterator
```

This `$offset_or_keys` argument can be

- a non empty array directly provided;
- a specific CSV row by providing its offset;

### Example 1 - Using an array to specify the keys

```php
use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$keys = ['firstname', 'lastname', 'email'];
$results = $reader->fetchAssoc($keys);
// $results is an iterator
foreach ($results as $row) {
// each row will have the following data
//       [
//             'firstname' => 'john',
//             'lastname' => 'doe',
//             'email' => 'john.doe@example.com',
//       ];
//
}
```

### Example 2 - Using a CSV offset

```php
$offset = 0;
$results = $reader->fetchAssoc($offset);
// $results is an iterator
foreach ($results as $row) {
// each row will have the following data
//     [
//         'john' => 'jane',
//         'doe' => 'doe',
//         'john.doe@example.com' => 'jane.doe@example.com',
//     ];
//
}
```

### Notes

- If the number of values in a CSV row is lesser than the number of named keys, the method will add `null` values to compensate for the missing values.
- If the number of values in a CSV row is greater that the number of named keys the exceeding values will be drop from the result set.
- If an offset is used, its content will be skipped in the result set.
- If no argument is provided, the first row from the CSV data will be used

### The optional callable argument

<p class="message-warning"><strong>BC Break:</strong> The <code>callable</code> expects a row with the indexes already applied to it.</p>

The method takes an optional callable which signature is as follow:

```php
function(array $row [, int $rowOffset [, Iterator $iterator]]): array
```

- `$row`: the CSV current row combined with the submitted indexes **(new in version 8.0.0)**
- `$rowOffset`: the CSV current row offset
- `$iterator`: the current CSV iterator

### Example 3 - Using a callable

```php
use League\Csv\Reader;

$func = function ($row) {
    $row['date'] = DateTimeImmutable::createFromFormat($row['date'], 'd-m-Y');

    return $row;
};
$keys = ['firstname', 'lastname', 'date'];
$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
foreach ($reader->fetchAssoc($keys, $func) as $row) {
    $row['date']->format('Y-m-d H:i:s');
    //because this cell contain a `DateTimeInterface` object
}
```

## Reader::fetchColumn

<p class="message-warning"><strong>BC Break:</strong> Starting with version <code>8.0.0</code> This method returns a <code>Iterator</code>.</p>

`fetchColumn` returns a `Iterator` of all values in a given column from the CSV data.

```php
public Reader::fetchColumn(
    int $columnIndex = 0,
    callable $callable = null
): Iterator
```

If for a given row the column does not exist, the row will be skipped.

### Example 1 - with a given column index

```php
use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
$result = $reader->fetchColumn(2);
$data = iterator_to_array($result, false);
// will return something like this :
//
// ['john.doe@example.com', 'jane.doe@example.com', ...]
//
```

### The optional callable argument

<p class="message-warning"><strong>BC Break:</strong> The <code>callable</code> expects the column value as its first parameter</p>

The method takes an optional callable which signature is as follow:

```php
function(string $value [, int $offsetIndex [, Iterator $iterator]]): mixed
```

- `$value`: the CSV current column value **(new to version 8.0.0)**
- `$offsetIndex`: the CSV current row offset
- `$iterator`: the current CSV iterator

### Example 2 - with a callable

```php
use League\Csv\Reader;

$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
foreach ($reader->fetchColumn(2, 'strtoupper') as $value) {
    echo $value; //display 'JOHN.DOE@EXAMPLE.COM'
}
```

## Reader::fetchPairs

<p class="message-notice">new feature introduced in version <code>8.0</code></p>

The `fetchPairs` method returns a `Generator` of key-value pairs.

```php
public Reader::fetchPairs(
    int $offsetIndex = 0,
    int $valueIndex = 1,
    callable $callable = null
): Generator
```

- The key is taken from the submitted column index parameter (ie: `$offsetIndex`).
- The value is taken from the submitted column value parameter (ie: `$valueIndex`).

### Example 1 - default usage

```php
use League\Csv\Reader;

$str = <<<EOF
john,doe
jane,doe
foo,bar
EOF;

$reader = Reader::createFromString($str);
foreach ($reader->fetchPairs() as $firstname => $lastname) {
    // - first iteration
    // echo $firstname; -> 'john'
    // echo $lastname;  -> 'doe'
    // - second iteration
    // echo $firstname; -> 'jane'
    // echo $lastname;  -> 'doe'
    // - third iteration
    // echo $firstname; -> 'foo'
    // echo $lastname; -> 'bar'
}
```

### Notes

- If no `$offsetIndex` is provided it defaults to `0`;
- If no `$valueIndex` is provided it defaults to `1`;
- If no cell is found corresponding to `$offsetIndex` the row is skipped;
- If no cell is found corresponding to `$valueIndex` the `null` value is used;

### The optional callable argument

The method takes an optional callable which signature is as follow:

```php
function(array $pairs [, int $rowOffset [, Iterator $iterator]]): array
```

- `$pairs`: an array where
  - the first value contains the value of the offset column index
  - the second value contains the value of the value column index
- `$rowOffset`: the CSV current row offset
- `$iterator`: the current CSV iterator

### Example 2 - with a callable

```php
use League\Csv\Reader;

$str = <<<EOF
john,doe
jane,doe
foo,bar
EOF;

$func = function ($row) {
    return [
        strtoupper($row[0]),
        strtolower($row[1]),
    ];
}
$reader = Reader::createFromString($str);
foreach ($reader->fetchPairs(1, 0, $func) as $lastname => $firstname) {
    // - first iteration
    // echo $lastname; -> 'DOE'
    // echo $firstname; -> 'john'
    // - second iteration
    // echo $lastname; -> 'DOE'
    // echo $firstname; -> 'jane'
    // - third iteration
    // echo $lastname; -> 'BAR'
    // echo $firstname; -> 'foo'
}
```

## Reader::fetchPairsWithoutDuplicates

<p class="message-notice">new feature introduced in version <code>8.0</code></p>

The `fetchPairsWithoutDuplicates` method returns data in an `array` of key-value pairs, as an associative array with a single entry per row.

```php
public Reader::fetchPairsWithoutDuplicates(
    int $offsetIndex = 0,
    int $valueIndex = 1,
    callable $callable = null
): array
```

`fetchPairsWithoutDuplicates` behaves exactly like `fetchPairs` with two differences:

- `fetchPairsWithoutDuplicates` returns an `array`
- When using `fetchPairsWithoutDuplicates` entries in the associative array will be overwritten if there are duplicates values in the column index.

```php
$str = <<<EOF
john,doe
jane,doe
foo,bar
EOF;

$reader = Reader::createFromString($str);
$data = $reader->fetchPairsWithoutDuplicates(1, 0);
// will return ['doe' => 'jane', 'foo' => 'bar'];
// the 'john' value has been overwritten by 'jane'
```