Download the PHP package cardinal-collections/array-with-secondary-keys without Composer

On this page you can find all versions of the php package cardinal-collections/array-with-secondary-keys. 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 array-with-secondary-keys

= Array with Secondary Keys

image:https://github.com/vrza/array-with-secondary-keys/actions/workflows/ci.yml/badge.svg[Build Status (GitHub Actions),link=https://github.com/vrza/array-with-secondary-keys/actions]

Wraps a PHP array and maintains secondary lookup maps, for fast retrieval of items by nested field values.

This is conceptually similar to how secondary keys in databases work.

== Features

Implements ArrayAccess, Iterator and Countable interfaces, so you can use it like you would a standard PHP array:

[source,php]

$a[] = ['foo' => 'bar']; $value = $a[0]; unset($a[0]); count($a); foreach ($a as $key => $value);

Implemented in pure PHP. Secondary indexes are implemented as PHP associative arrays -- hash tables with O(1) average lookup time.

Since the secondary index is a hash index, its keys are by design unique. While it is possible to have the same secondary key value in multiple documents in the collection, in such cases the secondary key will get overwritten and will point to the last added/modified document.

== API Reference

A "document" is the value associated to a primary key. It can be of any type. If the document is an array, secondary indexes can be specified using the "dot" notation.

All created secondary key indexes are automatically updated when adding/removing/updating values.

=== Constructor [source,php]

ArrayWithSecondaryKeys(array $array = [])

Creates a new ArrayWithSecondaryKeys collection, initialized from the given array. If called without arguments, an empty collection is created.

== containsPrimaryKey [source,php]

containsPrimaryKey($key): bool

Checks if the specified primary key is present in the collection.

== containsSecondaryKey [source,php]

containsSecondaryKey($index, $key): bool

Checks if the specified secondary key is present in the specified index.

=== count [source,php]

count(): int

Returns the number of documents in the collection.

=== get [source,php]

get($key, $default = null): mixed

Retrieves a document by key.

If the key is given in "dot" notation, the corresponding nested part of the document is returned. If the key is not a string that includes a dot, it is treated as a primary key.

=== has [source,php]

has($key): bool

Check if an item or items exist in the collection using "dot" notation.

=== isEmpty [source,php]

isEmpty(): bool

Returns true if there are no primary keys in the collection, false otherwise.

=== put [source,php]

put($key, $document): ArrayWithSecondaryKeys

Adds a new document to the collection under the given key.

If the key is given in "dot" notation, and the top level primary key exists, a nested part is added to the existing document.

If the key is not a string that includes a dot, it is treated as a primary key.

=== add [source,php]

add($key, $document): ArrayWithSecondaryKeys

An alias for put.

=== remove [source,php]

remove($key): ArrayWithSecondaryKeys

Removes the document associated with the given primary key, or removes a nested part of the document if key is given in "dot" notation.

If the key is given in "dot" notation, the corresponding nested part of the document is removed. If the key is not a string that includes a dot, it is treated as a primary key.

=== updateSecondaryKey [source,php]

updateSecondaryKey($index, $existingValue, $newValue)

Finds the document by existing value of secondary key and updates the existing value of secondary key in the document to new value.

Returns the primary key of the updated document, or null if a document with existing value is not found.

=== append [source,php]

append($document): ArrayWithSecondaryKeys

Appends a document (using []=).

=== createIndex [source,php]

createIndex($index)

Creates a new index and indexes existing documents. Index is given as string in "dot" notation (e.g. state.pid).

=== getPrimaryKeyByIndex [source,php]

getPrimaryKeyByIndex($index, $secondaryKey): mixed

Retrieves a primary key by secondary key. Index is given as string in "dot" notation (e.g. state.pid).

Returns null if no document in collection matches the given secondary key.

Throws VladimirVrzic\ArrayWithSecondaryKeys\NoSuchIndexException if the given index does not exist.

=== getByIndex [source,php]

getByIndex($index, $secondaryKey, $default = null): mixed

Retrieves a document by index (secondary key). Index is given as string in "dot" notation (e.g. state.pid).

Throws VladimirVrzic\ArrayWithSecondaryKeys\NoSuchIndexException if the given index does not exist.

=== updateByIndex [source,php]

updateByIndex($index, $secondaryKey, $document)

Updates a document by index (secondary key). Index is given as string in "dot" notation (e.g. state.pid).

Throws VladimirVrzic\ArrayWithSecondaryKeys\NoSuchIndexException if the given index does not exist.

Returns true if the existing document associated with the given secondary key was found and replaced. Returns false if the document associated with the given secondary key was not found.

=== removeByIndex [source,php]

removeByIndex($index, $secondaryKey)

Removes a document by index (secondary key). Index is given as string in "dot" notation (e.g. state.pid).

Throws VladimirVrzic\ArrayWithSecondaryKeys\NoSuchIndexException if the given index does not exist.

Returns true if the document associated with the given secondary key was found and removed. Returns false if a document associated with the given secondary key was not found.

=== putIfAbsent [source,php]

putIfAbsent($key, $document): mixed

If the key doesn't exist, adds the new key associated with the given document and returns null.

If the key exists, returns the current document.

=== asArray [source,php]

asArray(): array

Returns a copy of the array as a normal PHP array (without secondary indexes).

=== primaryKeys [source,php]

primaryKeys(): array

Returns an array of all primary keys.

=== secondaryKeys [source,php]

secondaryKeys($index): array

Returns an array of all secondary keys associated with the given index.

== Installation

Assuming you have PHP Composer installed, and that the composer executable is in your $PATH:

[source,shell]

composer require cardinal-collections/array-with-secondary-keys

== Name ideas


All versions of array-with-secondary-keys with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1
cardinal-collections/cardinal-collections Version ^1.0.0
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 cardinal-collections/array-with-secondary-keys contains the following files

Loading the files please wait ....