Download the PHP package seboettg/collection without Composer

On this page you can find all versions of the php package seboettg/collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package collection

PHP Total Downloads License Scrutinizer Code Quality Build Status Code Coverage Code Intelligence Status

Collection

Collection is a set of useful wrapper classes for arrays, similar to Java's or Kotlin's collection packages.

Table of Contents

  1. Versions
  2. Installing Collection
  3. Lists
    1. Getting started
    2. Iterate over lists
    3. List operations
    4. Map elements
    5. Filter elements
    6. Logical operations
    7. forEach
    8. sorting
  4. Maps
    1. Getting started
    2. Access elements
    3. Manipulate
    4. Map elements
    5. Filter map entries
  5. Combining Lists and Maps
  6. Stack
  7. Queue
  8. Contribution

Versions

Since Version 4.0 you need PHP 7.4 or higher to use this library. Since Version 2.1 you need PHP 7.1 to use Collection library. Previous versions are running from PHP 5.6 upwards.

Installing Collection

The recommended way to install Collection is through Composer.

Next, run the Composer command to install the latest stable version of Collection:

After installing, you need to require Composer's autoloader:

You can then later update Collection using composer:

Lists

List is an ordered collection with access to elements by indices – integer numbers that reflect their position. Elements can occur more than once in a list. In other words: a list can contain any number of equal objects or occurrences of a single object. Two lists are considered equal if they have the same sizes and structurally equal elements at the same positions.

Lists are completely new implemented for version 4.0. The handling is much more oriented on a functional approach. Further more methods for associative arrays are moved to map.

Getting started

Output

You also create a list from an existing array

Output

As you may notice, this will reset the array keys

You can also create an empty List:

output

Iterate over lists

Output:

or

output

List operations

You may add the elements of another list to a list by using plus:

output

The same operation is applicable with arrays, with the same result:

You can also subtract the elements of another list or any iterable using minus:

output

To get the intersection of two lists (or an iterable), you can use the intersect method:

output

To get a list containing distinct elements, use distinct:

output

Map all elements of a list

If you need to modify all elements in a list, you can do it easily by using the map method:

There is also a mapNotNull method that eliminates null values from the result:

Filter elements in a list

The filter method returns a list containing only elements matching the given predicate.

Logical operations

With the methods any and all you can check whether all elements (all) or at least one element (any) match a predicate.

forEach method

With the forEach method you can apply a closure or lambda functions on each element.

output:

Sorting a list

Implement the Comparable interface

Create a comparator class

Sort your list

sort your list using a custom order

Maps

A Map stores key-value pairs; keys are unique, but different keys can be paired with equal values. The Map interface provides specific methods, such as access to value by key, searching keys and values, and so on.

Getting started

A Map is a collection of keys that are paired with values. Therefore, to create a Map you need pairs first:

output

You can also create an empty Map:

output

Access elements

you are also able to get all map entries as a list of pairs

output

Manipulate a map

Map elements

The signature of given transform function for mapping must have either a Pair parameter or a key and a value parameter. The map function always returns a list of type ListInterface:

output

You get the same result with a key-value signature:

Filter entries of a map

You may filter for elements by this way:

or by this way:

Combining Lists and Maps

There are a lot of opportunities to use lists and maps in real world scenarios with a lot of advantages e.g. less boilerplate code and better code readability.

The following json file represents a customer file that we want to use for processing.

customer.json

We would like to get a map that associates the customer id with respective objects of type Customer and we want to apply a filter so that we get only customers with lastname Doe.

output

Another example: Assuming we have a customer service with a getCustomerById method. We have a list of IDs with which we want to request the service.

output

Stack

A stack is a collection of elements, with two principal operations:

  • push, which adds an element to the collection, and
  • pop, which removes the most recently added element that was not yet removed.

An Stack is a LIFO data structure: last in, first out.

Examples

push, pop and peek

search

The search function returns the position where an element is on this stack. If the passed element occurs as an element in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost element on the stack is considered to be at distance 1. If the passed element does not occur in the stack, this method returns 0.

Queue

A queue is a collection in which the elements are kept in order. A queue has two principle operations:

  • enqueue
  • dequeue

Examples

Contribution

Fork this Repo and feel free to contribute your ideas using pull requests.


All versions of collection with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package seboettg/collection contains the following files

Loading the files please wait ....