定制 yannoff/collections 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yannoff/collections

Composer 安装命令:

composer require yannoff/collections

包简介

A simple object implementation for PHP arrays

README 文档

README

A simple object implementation of PHP arrays.

Latest Stable Version Total Downloads License

The concept

Based upon the Decorator design pattern, the aim is to provide a flexible, object-oriented alternative to PHP Arrays.

Installation

Using composer:

$ composer require yannoff/collections

Usage

Example: PHP Array vs Collection

The classic way - native PHP arrays:

<?php

// Using PHP native arrays, the classic way:
$countries = [ 'France', 'Italia' ];

// Push an element
array_push($countries, 'Switzerland');

// Append an element
$countries[] = 'Belgium';

The new way - using collections:

<?php

use Yannoff\Component\Collections\Collection;

// Using collections:
$countries = new Collection([ 'France', 'Italia' ]);

// Push an element
$countries->push('Switzerland');

// Append an element: the same syntax as for arrays
$countries[] = 'Belgium';

// Exporting the collection back to a native PHP array:
$array = $countries->all();

// As for native arrays, collections can be traversed with a foreach:
foreach($countries as $key => $value) {
    // ...
}

Methods

See the full method list for more details.

Array / Collection method equivalences:

Array Collection
implode($glue, $array) $collection->join($glue)
array_filter($array, $callback, $flag) $collection->filter($callback, $flag)
array_flip($array) $collection->flip()
array_keys($array) $collection->keys()
array_map($callback, $array, ..$arrays) $collection->map($callback, ...$arrays)
array_pop(&$array) $collection->pop()
array_push(&$array, $element) $collection->push($element)
array_reverse($array) $collection->reverse()
array_search($needle, $array) $collection->search($needle)
array_slice($array, $offset, $length) $collection->slice($offset, $length)
array_shift(&$array) $collection->shift()
array_unique($array, $flags) $collection->unique($flags)
array_unshift(&$array, $element) $collection->unshift($element)
array_walk(&$array, $callback, $userdata) $collection->walk($callback, $userdata)
explode($separator, $strint, $limit) Collection::explode($separator, $strint, $limit)
current(&$array) $collection->current()
end(&$array) $collection->end()
next(&$array) $collection->next()
prev(&$array) $collection->prev()
sort(&$array) $collection->sort()
asort(&$array) $collection->asort()
ksort(&$array) $collection->ksort()

Along with those classical PHP methods wrappers, a few bag methods are provided:

method description
add($key, $element) Add an element at the given key in the collection
all() Return all the collection elements
clear() Remove all elements from the collection
get($key) Get an element of the collection by its key
has($key) Check for existence of the given key in the collection
set($elements) Set the whole elements of the collection

License

Released under the MIT License

统计信息

  • 总下载量: 492
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-05-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固