affinity4/collection

具有实用辅助方法的可迭代集合。掌握你的数组!

1.0.0 2019-06-01 22:33 UTC

This package is auto-updated.

Last update: 2024-09-26 18:22:16 UTC


README

具有实用辅助方法的可迭代集合。掌握你的数组!

安装

composer require affinity4/collection

使用方法

标准可迭代API

require_once __DIR__ '/vendor/autoload.php';

use Affinity4\Collection;

$Collection = new Collection([
    0 => 'one',
    1 => 'two',
    2 => 'three'
]);

$Collection->key(); // 0
$Collection->current(); // one
$Collection->valid(); // true

$Collection->next();
$Collection->key(); // 1
$Collection->valid(); // true
$Collection->current(); // two

$Collection->next();
$Collection->key(); // 2
$Collection->valid(); // true
$Collection->current(); // three

$Collection->next();
$Collection->key(); // 3
$Collection->valid(); // false

$Collection->prev();
$Collection->key(); // 2
$Collection->valid(); // true
$Collection->current(); // three

$Collection->rewind();
$Collection->key(); // 0
$Collection->valid(); // true
$Collection->current(); // one

标准数组访问API

require_once __DIR__ '/vendor/autoload.php';

use Affinity4\Collection;

$Collection = new Collection([
    0       => 'one',
    1       => 'two',
    2       => 'three',
    'one'   => 1,
    'two'   => 2,
    'three' => 3
]);

$Collection[0]; // one
$Collection[1]; // two
$Collection[2]; // three

$Collection['one'];   // 1
$Collection['two'];   // 2
$Collection['three']; // 3