php 示例
最后更新于:2022-04-02 04:20:48
[TOC]
## 示例
### 概念示例
输出 ``` Straight traversal: First Second Third Reverse traversal: Third Second First ``` ### 获取cvs
输出 ``` Array ( [0] => Name [1] => Age [2] => Owner [3] => Breed [4] => Image [5] => Color [6] => Texture [7] => Fur [8] => Size ) Array ( [0] => Steve [1] => 3 [2] => Alexander Shvets [3] => Bengal [4] => /cats/bengal.jpg [5] => Brown [6] => Stripes [7] => Short [8] => Medium ) Array ( [0] => Siri [1] => 2 [2] => Alexander Shvets [3] => Domestic short-haired [4] => /cats/domestic-sh.jpg [5] => Black [6] => Solid [7] => Medium [8] => Medium ) Array ( [0] => Fluffy [1] => 5 [2] => John Smith [3] => Maine Coon [4] => /cats/Maine-Coon.jpg [5] => Gray [6] => Stripes [7] => Long [8] => Large ) ```
';
main.php
``` collection = $collection; $this->reverse = $reverse; } public function rewind() { $this->position = $this->reverse ? count($this->collection->getItems()) - 1 : 0; } public function current() { return $this->collection->getItems()[$this->position]; } public function key() { return $this->position; } public function next() { $this->position = $this->position + ($this->reverse ? -1 : 1); } public function valid() { return isset($this->collection->getItems()[$this->position]); } } class WordsCollection implements \IteratorAggregate { private $items = []; public function getItems() { return $this->items; } public function addItem($item) { $this->items[] = $item; } public function getIterator(): Iterator { return new AlphabeticalOrderIterator($this); } public function getReverseIterator(): Iterator { return new AlphabeticalOrderIterator($this, true); } } $collection = new WordsCollection(); $collection->addItem("First"); $collection->addItem("Second"); $collection->addItem("Third"); echo "Straight traversal:\n"; foreach ($collection->getIterator() as $item) { echo $item . "\n"; } echo "\n"; echo "Reverse traversal:\n"; foreach ($collection->getReverseIterator() as $item) { echo $item . "\n"; } ```输出 ``` Straight traversal: First Second Third Reverse traversal: Third Second First ``` ### 获取cvs
main.php
``` filePointer = fopen($file, 'rb'); $this->delimiter = $delimiter; } catch (Exception $e) { throw new Exception('The file "' . $file . '" cannot be read.'); } } public function rewind(): void { $this->rowCounter = 0; rewind($this->filePointer); } /** * * @return array The current CSV row as a 2-dimensional array. */ public function current(): array { $this->currentElement = fgetcsv($this->filePointer, self::ROW_SIZE, $this->delimiter); $this->rowCounter++; return $this->currentElement; } /** * * @return int The current row number. */ public function key(): int { return $this->rowCounter; } /** * @return bool Returns true on EOF reached, false otherwise. */ public function next(): bool { if (is_resource($this->filePointer)) { return !feof($this->filePointer); } return false; } /** * @return bool If the next row is a valid row. */ public function valid(): bool { if (!$this->next()) { if (is_resource($this->filePointer)) { fclose($this->filePointer); } return false; } return true; } } $csv = new CsvIterator(__DIR__ . '/cats.csv'); foreach ($csv as $key => $row) { print_r($row); } ```输出 ``` Array ( [0] => Name [1] => Age [2] => Owner [3] => Breed [4] => Image [5] => Color [6] => Texture [7] => Fur [8] => Size ) Array ( [0] => Steve [1] => 3 [2] => Alexander Shvets [3] => Bengal [4] => /cats/bengal.jpg [5] => Brown [6] => Stripes [7] => Short [8] => Medium ) Array ( [0] => Siri [1] => 2 [2] => Alexander Shvets [3] => Domestic short-haired [4] => /cats/domestic-sh.jpg [5] => Black [6] => Solid [7] => Medium [8] => Medium ) Array ( [0] => Fluffy [1] => 5 [2] => John Smith [3] => Maine Coon [4] => /cats/Maine-Coon.jpg [5] => Gray [6] => Stripes [7] => Long [8] => Large ) ```