PHP Iterables

3 min read

PHP – What is an Iterable? #

PHP – Давталттай гэж юу вэ? #

An iterable is any value which can be looped through with a foreach() loop.

Давталт гэдэг нь foreach() давталтаар дамжуулж болох аливаа утгыг хэлнэ.

The iterable pseudo-type was introduced in PHP 7.1, and it can be used as a data type for function arguments and function return values.

iterable псевдо төрлийг PHP 7.1-д нэвтрүүлсэн бөгөөд үүнийг функцын аргумент болон функцийн өгөөжийн утгын өгөгдлийн төрөл болгон ашиглаж болно.


PHP – Using Iterables #

The iterable keyword can be used as a data type of a function argument or as the return type of a function:

iterable түлхүүр үгийг функцын аргументийн өгөгдлийн төрөл эсвэл функцийн буцах төрөл болгон ашиглаж болно.

Example #

Жишээ #

Use an iterable function argument:

Давтагдах функцын аргументыг ашиглана уу:

 
<?php function printIterable(iterable $myIterable) { foreach($myIterable as $item) { echo $item; } } $arr = ["a", "b", "c"]; printIterable($arr); ?>

Example #

Жишээ #

Return an iterable:

Давталттай зүйлийг буцаах:

<?php function getIterable():iterable { return ["a", "b", "c"]; } $myIterable = getIterable(); foreach($myIterable as $item) { echo $item; } ?>

PHP – Creating Iterables #

Arrays Багцууд

All arrays are iterables, so any array can be used as an argument of a function that requires an iterable.

Бүх багцууд нь давтагдах боломжтой тул дурын массивыг давталт шаардагдах функцийн аргумент болгон ашиглаж болно.

Iterators

Any object that implements the Iterator interface can be used as an argument of a function that requires an iterable.

Iterator интерфейсийг хэрэгжүүлж буй аливаа объектыг давталт шаарддаг функцийн аргумент болгон ашиглаж болно.

An iterator contains a list of items and provides methods to loop through them. It keeps a pointer to one of the elements in the list. Each item in the list should have a key which can be used to find the item.

Давталт нь зүйлсийн жагсаалтыг агуулдаг бөгөөд тэдгээрийг давтах аргуудыг өгдөг. Энэ нь жагсаалтад байгаа элементүүдийн аль нэгэнд заагчийг хадгалдаг. Жагсаалтын зүйл тус бүрт тухайн зүйлийг олоход ашиглаж болох түлхүүр байх ёстой.

An iterator must have these methods:

Давталжуулагч нь эдгээр аргуудтай байх ёстой.

  • current() – Returns the element that the pointer is currently pointing to. It can be any data type

    Заагчийн зааж байгаа элементийг буцаана. Энэ нь ямар ч өгөгдлийн төрөл байж болно

  • key() Returns the key associated with the current element in the list. It can only be an integer, float, boolean or string

    Жагсаалтын одоогийн элементтэй холбоотой түлхүүрийг буцаана. Энэ нь зөвхөн бүхэл тоо, хөвөгч, бул эсвэл мөр байж болно

  • next() Moves the pointer to the next element in the list

    Жагсаалтын дараагийн элемент рүү заагчийг шилжүүлнэ

  • rewind() Moves the pointer to the first element in the list

    Жагсаалтын эхний элемент рүү заагчийг шилжүүлнэ

  • valid() If the internal pointer is not pointing to any element (for example, if next() was called at the end of the list), this should return false. It returns true in any other case

    Хэрэв дотоод заагч нь ямар ч элемент рүү заагаагүй бол (жишээлбэл, жагсаалтын төгсгөлд next () дуудагдсан бол), энэ нь false гэсэн утгатай байх ёстой. Энэ нь бусад тохиолдолд үнэнээр эргэж ирдэг

Example #

Жишээ #

Implement the Iterator interface and use it as an iterable:

Iterator интерфэйсийг хэрэгжүүлж, үүнийг давталт болгон ашигла:

items = array_values($items); } public function current() { return $this->items[$this->pointer]; } public function key() { return $this->pointer; } public function next() { $this->pointer++; } public function rewind() { $this->pointer = 0; } public function valid() { // count() indicates how many items are in the list return $this->pointer < count($this->items); } } // A function that uses iterables function printIterable(iterable $myIterable) { foreach($myIterable as $item) { echo $item; } } // Use the iterator as an iterable $iterator = new MyIterator(["a", "b", "c"]); printIterable($iterator); ?>

Powered by BetterDocs

Leave a Reply