summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-03-03 09:15:00 +0100
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-03-03 09:15:00 +0100
commite79b1411559508b4111f649ac0618eeaa756d7c9 (patch)
tree4fd5b53cf91da3d52c38438f377a0d67646074a3 /src
parent865d138a7a7895b62af9e5aaa7ca1644a19241de (diff)
downloadcsv-e79b1411559508b4111f649ac0618eeaa756d7c9.zip
csv-e79b1411559508b4111f649ac0618eeaa756d7c9.tar.gz
csv-e79b1411559508b4111f649ac0618eeaa756d7c9.tar.bz2
improve fetchAssoc
Diffstat (limited to 'src')
-rw-r--r--src/Reader.php36
1 files changed, 8 insertions, 28 deletions
diff --git a/src/Reader.php b/src/Reader.php
index d01e60d..bf3e37f 100644
--- a/src/Reader.php
+++ b/src/Reader.php
@@ -176,11 +176,15 @@ class Reader extends AbstractCsv
*/
public function fetchAssoc($offset_or_keys = 0, callable $callable = null)
{
- $keys = $this->getAssocKeys($offset_or_keys);
+ $keys = $this->getAssocKeys($offset_or_keys);
+ $keys_count = count($keys);
+ $iterator = $this->query($callable);
+ $iterator = new Modifier\MapIterator($iterator, function (array $row) use ($keys, $keys_count) {
+ if ($keys_count != count($row)) {
+ $row = array_slice(array_pad($row, $keys_count, null), 0, $keys_count);
+ }
- $iterator = $this->query($callable);
- $iterator = new Modifier\MapIterator($iterator, function ($row) use ($keys) {
- return static::combineArray($keys, $row);
+ return array_combine($keys, $row);
});
return iterator_to_array($iterator, false);
@@ -252,28 +256,4 @@ class Reader extends AbstractCsv
return is_scalar($value) || (is_object($value) && method_exists($value, '__toString'));
}));
}
-
- /**
- * Intelligent Array Combine
- *
- * add or remove values from the $value array to
- * match array $keys length before using PHP array_combine function
- *
- * @param array $keys
- * @param array $value
- *
- * @return array
- */
- protected static function combineArray(array $keys, array $value)
- {
- $nbKeys = count($keys);
- $diff = $nbKeys - count($value);
- if ($diff > 0) {
- $value = array_merge($value, array_fill(0, $diff, null));
- } elseif ($diff < 0) {
- $value = array_slice($value, 0, $nbKeys);
- }
-
- return array_combine($keys, $value);
- }
}