All Projects → CismonX → Ext Collections

CismonX / Ext Collections

Licence: mit
Array manipulation extension for PHP.

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Ext Collections

Php Extensions
PHP C扩展开发
Stars: ✭ 31 (-67.02%)
Mutual labels:  php-extension
Phptdlib
PHP Extension for tdlib/td written with PHP-CPP
Stars: ✭ 59 (-37.23%)
Mutual labels:  php-extension
Php Rs
A library to build PHP extensions in Rust.
Stars: ✭ 81 (-13.83%)
Mutual labels:  php-extension
Php Spx
A simple & straight-to-the-point PHP profiling extension with its built-in web UI
Stars: ✭ 972 (+934.04%)
Mutual labels:  php-extension
Cdcontainers
Library of data containers and data structures for C programming language.
Stars: ✭ 57 (-39.36%)
Mutual labels:  collections
Seaslog
An effective,fast,stable log extension for PHP.http://pecl.php.net/package/SeasLog http://php.net/SeasLog
Stars: ✭ 1,136 (+1108.51%)
Mutual labels:  php-extension
Xhprof Apm
Xhprof APM
Stars: ✭ 29 (-69.15%)
Mutual labels:  php-extension
Php Ext Collection
PHP collection extensions - PHP Version 7.x
Stars: ✭ 89 (-5.32%)
Mutual labels:  php-extension
Amazing Windows Apps
📗Introduce you amazing Windows apps🕶READ ONLINE 👉
Stars: ✭ 1,102 (+1072.34%)
Mutual labels:  collections
Gutenberg Parser Rs
An experimental Rust parser for WordPress Gutenberg post format
Stars: ✭ 76 (-19.15%)
Mutual labels:  php-extension
Mtgdesktopcompanion
Cards manager for magic the gathering
Stars: ✭ 44 (-53.19%)
Mutual labels:  collections
Phpcollections
A set of collections for PHP.
Stars: ✭ 53 (-43.62%)
Mutual labels:  collections
Php Ion
Asynchronous PHP
Stars: ✭ 65 (-30.85%)
Mutual labels:  php-extension
Collecterator
Generator based PHP Collections
Stars: ✭ 33 (-64.89%)
Mutual labels:  collections
Ant php extension
PHP 扩展, 用于 PHP-FPM、FastCGI、LD_PRELOAD等模式下突破 disabled_functions
Stars: ✭ 85 (-9.57%)
Mutual labels:  php-extension
Graphql Parser Php
A PHP extension wrapping the libgraphqlparser library for parsing GraphQL.
Stars: ✭ 29 (-69.15%)
Mutual labels:  php-extension
Buckets Js
A complete, fully tested and documented data structure library written in pure JavaScript.
Stars: ✭ 1,128 (+1100%)
Mutual labels:  collections
Ip2region
Ip2region is a offline IP location library with accuracy rate of 99.9% and 0.0x millseconds searching performance. DB file is ONLY a few megabytes with all IP address stored. binding for Java,PHP,C,Python,Nodejs,Golang,C#,lua. Binary,B-tree,Memory searching algorithm
Stars: ✭ 9,836 (+10363.83%)
Mutual labels:  php-extension
Vvedenie Mashinnoe Obuchenie
📝 Подборка ресурсов по машинному обучению
Stars: ✭ 1,282 (+1263.83%)
Mutual labels:  collections
Cyclops
An advanced, but easy to use, platform for writing functional applications in Java 8.
Stars: ✭ 1,180 (+1155.32%)
Mutual labels:  collections

ext-collections

Travis-CI Codecov MIT license

1. Introduction

This PHP extension provides a set of useful and convenient operations on PHP arrays, which makes working with arrays simple and scalable.

Method names and functionalities are inspired by Kotlin.Collections, having a slightly different style than that of Laravel Collections.

Requires PHP version >= 7.1 and < 8.0 (master branch).

1.1 Example

Here is a simple example on how to work with arrays gracefully using this extension.

$employees = [
    ['name' => 'Alice',    'gender' => 'female', 'age' => 35],
    ['name' => 'Bob',      'gender' => 'male',   'age' => 29],
    ['name' => 'David',    'gender' => 'male',   'age' => 40],
    ['name' => 'Benjamin', 'gender' => 'male',   'age' => 32]
];
// Trying to get an array of names of male employees,
// sorted by the descending order of their age.
$names = Collection::init($employees)
    ->filter(function ($value) {
        return $value['gender'] == 'male';
    })
    ->sortedByDescending(function ($value) {
        return $value['age'];
    })
    ->map(function ($value) {
        return $value['name'];
    })
    ->toArray();
// You got $names == ['David', 'Benjamin', 'Bob'].

2. Getting Started

2.1 Installation

Like other PHP extensions, ext-collections can be built and installed with a few commands:

phpize
./configure
make
sudo make install

Include it in your PHP configuration file to enable this extension:

extension=collections.so

Building on Windows is not as convenient, however, pre-built binaries for Windows are provided in the releases. If you want to build it yourself, follow the official PHP wiki.

2.2 API Reference

See stubs directory for signatures of all classes and methods of this extension, with PHPDoc. They can also serve as IDE helper.

2.3 PHP-style Access

The Collection class implements ArrayAccess and Countable interface internally, you can treat an instance of Collection as an ArrayObject.

  • The isset(), unset() keywords can be used on elements of Collection.
  • Elements can be accessed via property or bracket expression.
  • empty(), count() can be used on instance of Collection.
  • Elements can be traversed via foreach() keyword.

3. Notes

  • The Collection::xxxTo() methods will preserve the original key-value pairs of destination Collection when keys collide.
  • Some methods of Collection involves comparing two of its elements, which accepts $flags as one of its arguments. When these methods are being invoked, make sure all elements are of the same type (numeric/string/others), otherwise you're likely to get a segfault.

3.1 Copy-on-write Mechanism

Class Collection does not introduce new data structures internally. Instead, it only holds a pointer to a zend_array, and all its methods works directly on top of zend_array. Which means conversion between Collection and array does not involve copying, until write operation is performed on one of the duplicates.

$foo = ['a', 'b'];              // arr0: refcount = 1
$bar = Collection::init($foo);  // arr0: refcount = 2, no copying of either `zend_array` or its elements
echo $bar->containsValue('a');  // arr0: refcount = 2, read operation, no copying
$bar->shuffle();                // arr0: refcount = 1, arr1: refcount = 1, write operation, `zend_array` is separated
$baz = $bar->toArray();         // arr0: refcount = 1, arr1: refcount = 2, no copying
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].