class Container (View source)

Container utility class

Constants

MODIFIABLE

Container is modifiable.

Container property flag.

EXTENDABLE

Container can accept new elements.

Container property flag.

SHRINKABLE

Elements can be removed from container

Container property flag.

TRAVERSABLE

Container can is traversable.

Container property flag.

COUNTABLE

Number of elements contained is available

Container property flag.

RANDOM_ACCESS

Container elements can be accessed using a arbitrary random access method.

Container property flag.

PROPERTY_ACCESS

Container elements can be accessed by using the arrow operator.

Container property flag.

OFFSET_ACCESS

Container elements can be accessed by using the bracket operator.

Container property flag.

IMPLODE_KEYS

The container element key will be passed to the user-defined callable.

Container::implode() function callable argument selection flag.

IMPLODE_VALUES

The container element value will be passed to the user-defined callable;

Container::implode() function callable argument selection flag.

IMPLODE_BEFORE

String to prepend before each array element.

IMPLODE_AFTER

String to append after each array element.

IMPLODE_BETWEEN

String to insert between two elements of the array.

IMPLODE_BETWEEN_LAST

String to insert between the penultimate and last element of the array.

MERGE_RECURSE

MERGE_LIST_REPLACE

Methods

static integer
properties(mixed $container, bool $fromContainerPropertyInterface = false)

Get the kind of operation supported by a given container

static 
isArray(mixed $container)

Indicates if the parameter is an array or an object which implements ArrayAccess interface (PHP 5)

static boolean
isTraversable($container)

Indicates if the parameter is an array or an object which

static array
keys(mixed $container)

Get the list of keys of the given container

static boolean
isEmpty(mixed $container)

No description

static mixed[]
values(mixed $container)

Get the list of values of the given container.

static boolean
removeKey(mixed $container, mixed $key)

Remove an entry from the given container

static mixed
shift(mixed $container)

Remove first entry from container and return its value.

static mixed
pop(mixed $container)

Remove last entry from container and return its value.

static array
createArray(mixed $anything, number $singleElementKey = null)

Transform any type to a plain PHP array

static boolean
isIndexed(mixed $container, boolean $strict = false, boolean $allowEmpty = true)

Indicates if the given container could be considered as an indexed array

static The
nthValue(mixed $container, integer $offset, mixed $dflt = null)

Iterate container and return the value of the nth element.

static boolean
isAssociative(array|ArrayAccess|Traversable $container, boolean $strict = false, boolean $allowEmpty = true)

Indicates if the given array is an associative array

static 
count($container)

No description

static 
reset(array|ArrayAccess $container)

Reset array pointer to initial value or rewind an Iterator

static boolean
keyExists(array|ArrayAccess|Traversable $container, mixed $key)

Indicates if a key exists in an array or a ArrayAccess implementation

static boolean
valueExists(array|ArrayAccess|Traversable $container, mixed $value, boolean $strict = false)

Indicates if the given value appears in the container elements

static array
first($container, $dflt = array())

Get the first key and value of the given container.

static mixed
firstKey($container, $dflt = null)

Get the first key of the given container

static mixed
firstValue(mixed $container, mixed $dflt = null)

Get the first value of the container.

static array
last(mixed $container, array $dflt = array())

Get the last key-value pair in the given container

static mixed
lastKey($container, $dflt = null)

Get the last key of the given container

static mixed
lastValue(mixed $container, mixed $dflt = null)

Get the last value of the container.

static mixed
keyValue(array $container, integer|string $key, mixed $defaultValue = null)

Retrieve key value or a default value if key doesn't exists

static mixed
treeValue($container, $keyTree, $dflt = null, $keySeparator = '.')

No description

static 
setValue(array|ArrayAccess|Traversable $container, mixed $key, mixed $value)

No description

static 
appendValue(array|ArrayAccess|object $container, mixed $value)

Add a value at the end of the container value list.

static 
prependValue(array|ArrayObject|object $container, mixed $value)

Add a value at the beginning of the container value list.

static string
implodeValues($container, $glue, $callable = null, $callableArguments = array())

Implode array values

static string
implodeKeys($container, $glue, $callable = null, $callableArguments = array())

Implode array keys

static string
implode(array $container, string $glue, callable $callable, string $callableArguments = array())

Implode an array

static array
filter(array|Traversable $container, callable $callable)

No description

static array
filterValues(array|Traversable $container, callable $callable)

Filter container elements by values

static array
filterKeys(array|Traversable $container, callable $callable)

Filter container elements by keys

static array
map(mixed $container, callable $callable)

Returns an array containing the results of applying the callback to each of input container elements.

static array
mapValues(mixed $container, callable $callable)

Returns an array containing the results of applying the callback to each of input container elements.

static $container
walk(mixed $container, callable $callable)

Applies the user-defined callback function to each element of the container $container.

static boolean
asort(array|ArrayObject $container, integer $flags = \SORT_REGULAR)

Sort array values and maintain index association

static boolean
ksort(array|ArrayObject $container, integer $flags = \SORT_REGULAR)

Sort an array by keys

static boolean
natsort(array|ArrayObject $container)

Sort an array using a "natural order" algorithm

static boolean
natcasesort(array|ArrayObject $container)

Sort an array using a case insensitive "natural order" algorithm

static boolean
uasort(array|ArrayObject $container, callable $callable)

Sort an array with a user-defined comparison function and maintain index associatio

static boolean
uksort(array|ArrayObject $container, callable $callable)

Sort an array by keys using a user-defined comparison function

static mixed[]
uniqueValues(unknown $container, unknown $comparer = null)

Removes duplicate values from an container

static array
merge()

Merge two or more container

static 
defaultElementComparer($a, $b)

No description

Details

static integer properties(mixed $container, bool $fromContainerPropertyInterface = false)

Get the kind of operation supported by a given container

Parameters

mixed $container Container
bool $fromContainerPropertyInterface Indicates that the method is invokedee from the ContainerPropertyInterface::getContainerProperties().

Return Value

integer A combination of the following flags

<

ul>

  • Container::RANDOM_ACCESS
  • Container::TRAVERSABLE
  • Container::MODIFIABLE
  • Container::EXTENDABLE
  • Container::SHRINKABLE
  • Container::RANDOM_ACCESS
  • Container::PROPERTY_ACCESS
  • Container::OFFSET_ACCESS
    • static isArray(mixed $container)

      Indicates if the parameter is an array or an object which implements ArrayAccess interface (PHP 5)

      Parameters

      mixed $container The container

      static boolean isTraversable($container)

      Indicates if the parameter is an array or an object which

      Parameters

      $container

      Return Value

      boolean true if$container is traversable (i.e usable in a roreach statement)

      static array keys(mixed $container)

      Get the list of keys of the given container

      Parameters

      mixed $container Input container

      Return Value

      array Numerically indexed array of keys used in $container

      Exceptions

      InvalidContainerException

      static boolean isEmpty(mixed $container)

      Parameters

      mixed $container Input container

      Return Value

      boolean true if $container does not contains any element

      static mixed[] values(mixed $container)

      Get the list of values of the given container.

      Parameters

      mixed $container Input container

      Return Value

      mixed[] Numerically indexed array of values contained in $container

      Exceptions

      InvalidContainerException

      static boolean removeKey(mixed $container, mixed $key)

      Remove an entry from the given container

      Parameters

      mixed $container The container
      mixed $key Entry key to remove

      Return Value

      boolean true if $container was modified

      Exceptions

      InvalidContainerException

      static mixed shift(mixed $container)

      Remove first entry from container and return its value.

      Parameters

      mixed $container The container

      Return Value

      mixed Value of the first entry of the container

      Exceptions

      EmptyContainerException

      static mixed pop(mixed $container)

      Remove last entry from container and return its value.

      Parameters

      mixed $container The container

      Return Value

      mixed Value of the last entry of the container

      Exceptions

      EmptyContainerException

      static array createArray(mixed $anything, number $singleElementKey = null)

      Transform any type to a plain PHP array

      Parameters

      mixed $anything The container
      number $singleElementKey Key used to create a single element array when is not something that could be converted to an array

      Return Value

      array or null if $anything cannont be converted to array and $singleElementKey is null

      static boolean isIndexed(mixed $container, boolean $strict = false, boolean $allowEmpty = true)

      Indicates if the given container could be considered as an indexed array

      An indexed array is a container where keys are a sequence of integers starting from 0 to n-1. (where n is the number of elements of the container)

      An empty container is always considered as an indexed array

      Complexity : 𝛰(n)

      Parameters

      mixed $container Any traversable container
      boolean $strict Only accept pure integer type as valid key. Otherwise, a string key containing only digits is accepted.
      boolean $allowEmpty Assumes empty array is indexed

      Return Value

      boolean true if the container keys is a non-sparse sequence of integer starting from 0 to n-1 (where n is the number of elements of the container).

      static The nthValue(mixed $container, integer $offset, mixed $dflt = null)

      Iterate container and return the value of the nth element.

      Parameters

      mixed $container A tranversable container
      integer $offset Index of the expected value
      mixed $dflt Value to return if the expected index does not exists

      Return Value

      The nth value of the container

      Exceptions

      InvalidContainerException

      static boolean isAssociative(array|ArrayAccess|Traversable $container, boolean $strict = false, boolean $allowEmpty = true)

      Indicates if the given array is an associative array

      An empty container is always considered as associative

      Complexity : 𝛰(n)

      Parameters

      array|ArrayAccess|Traversable $container Any traversable container
      boolean $strict If true, only consider
      boolean $allowEmpty Assumes empty array is associative

      Return Value

      boolean @true if at least one of $container keys is not a integer or if the array keys are not consecutive values. An empty container is considered as associative

      Exceptions

      InvalidContainerException

      static count($container)

      Parameters

      $container

      static reset(array|ArrayAccess $container)

      Reset array pointer to initial value or rewind an Iterator

      Parameters

      array|ArrayAccess $container The container

      Exceptions

      InvalidContainerException

      static boolean keyExists(array|ArrayAccess|Traversable $container, mixed $key)

      Indicates if a key exists in an array or a ArrayAccess implementation

      Parameters

      array|ArrayAccess|Traversable $container The container
      mixed $key Key to test

      Return Value

      boolean

      Exceptions

      InvalidContainerException

      static boolean valueExists(array|ArrayAccess|Traversable $container, mixed $value, boolean $strict = false)

      Indicates if the given value appears in the container elements

      Parameters

      array|ArrayAccess|Traversable $container The container
      mixed $value Value to check in $container
      boolean $strict If true, use the strict equal (===) operator

      Return Value

      boolean true if $value appears in $container

      Exceptions

      InvalidContainerException

      static array first($container, $dflt = array())

      Get the first key and value of the given container.

      Parameters

      $container
      $dflt

      Return Value

      array Array of two elements containing the first key and value of the given container. If the container is empty, both return values will be NULL.

          The list() function can be used to get the result of this function.
      

      Exceptions

      InvalidContainerException

      static mixed firstKey($container, $dflt = null)

      Get the first key of the given container

      Parameters

      $container
      $dflt

      Return Value

      mixed

      static mixed firstValue(mixed $container, mixed $dflt = null)

      Get the first value of the container.

      Parameters

      mixed $container The container
      mixed $dflt Value to return if $container is empty

      Return Value

      mixed

      static array last(mixed $container, array $dflt = array())

      Get the last key-value pair in the given container

      Parameters

      mixed $container Container
      array $dflt Default key-value to return if container is empty

      Return Value

      array

      Exceptions

      InvalidContainerException

      static mixed lastKey($container, $dflt = null)

      Get the last key of the given container

      Parameters

      $container
      $dflt

      Return Value

      mixed

      static mixed lastValue(mixed $container, mixed $dflt = null)

      Get the last value of the container.

      Parameters

      mixed $container The container
      mixed $dflt Value to return if $container is empty

      Return Value

      mixed

      static mixed keyValue(array $container, integer|string $key, mixed $defaultValue = null)

      Retrieve key value or a default value if key doesn't exists

      Parameters

      array $container The container
      integer|string $key Container index or key
      mixed $defaultValue Value to return if $key does not exists in container.

      Return Value

      mixed Value associated to $key or $defaultValue if the key does not exists

      Exceptions

      InvalidContainerException

      static mixed treeValue($container, $keyTree, $dflt = null, $keySeparator = '.')

      Parameters

      $container
      $keyTree
      $dflt
      $keySeparator

      Return Value

      mixed Value associated to $keyTree or $defaultValue if the key does not exists

      Exceptions

      InvalidContainerException

      static setValue(array|ArrayAccess|Traversable $container, mixed $key, mixed $value)

      Parameters

      array|ArrayAccess|Traversable $container The container
      mixed $key Container index or key
      mixed $value Value to set.

      Exceptions

      InvalidArgumentException
      InvalidContainerException

      static appendValue(array|ArrayAccess|object $container, mixed $value)

      Add a value at the end of the container value list.

      Parameters

      array|ArrayAccess|object $container The container
      mixed $value Value to append to the end of the container value list.

      Exceptions

      InvalidContainerException
      Exception

      static prependValue(array|ArrayObject|object $container, mixed $value)

      Add a value at the beginning of the container value list.

      Parameters

      array|ArrayObject|object $container The container
      mixed $value Value to add at the beginning of the container value list.

      Exceptions

      InvalidContainerException
      Exception

      static string implodeValues($container, $glue, $callable = null, $callableArguments = array())

      Implode array values

      Parameters

      $container
      $glue
      $callable
      $callableArguments

      Return Value

      string

      static string implodeKeys($container, $glue, $callable = null, $callableArguments = array())

      Implode array keys

      Parameters

      $container
      $glue
      $callable
      $callableArguments

      Return Value

      string

      static string implode(array $container, string $glue, callable $callable, string $callableArguments = array())

      Implode an array

      Parameters

      array $container Array to implode
      string $glue Glue
      callable $callable Function to apply to all entries. Function receive key and value as first arguments.
      string $callableArguments Additional arguments passed to $callable

      Return Value

      string

      static array filter(array|Traversable $container, callable $callable)

      Parameters

      array|Traversable $container The container
      callable $callable Filter callable invoked for each element o.f $container. The prototype must be function ($key, $value) : boolean

      Return Value

      array Filtered container

      static array filterValues(array|Traversable $container, callable $callable)

      Filter container elements by values

      Parameters

      array|Traversable $container Container to filter
      callable $callable Filter callable invoked for each element of $container. The prototype must be function ($value) : boolean

      Return Value

      array Filtered container

      static array filterKeys(array|Traversable $container, callable $callable)

      Filter container elements by keys

      Parameters

      array|Traversable $container Container to filter
      callable $callable Filter callable invoked for each element of $container. The prototype must be function ($key) : boolean

      Return Value

      array Filtered container

      static array map(mixed $container, callable $callable)

      Returns an array containing the results of applying the callback to each of input container elements.

      Parameters

      mixed $container Container
      callable $callable Callable to apply on each $container elements

      Return Value

      array

      Exceptions

      InvalidContainerException

      See also

      https://www.php.net/manual/en/function.array-map.php

      static array mapValues(mixed $container, callable $callable)

      Returns an array containing the results of applying the callback to each of input container elements.

      Parameters

      mixed $container Container
      callable $callable Callable to apply on each $container elements. The callable receives as arguments the current array element value and all additional arguments given to the mapValues() method

      Return Value

      array

      Exceptions

      InvalidContainerException

      See also

      https://www.php.net/manual/en/function.array-map.phpValueValue

      static $container walk(mixed $container, callable $callable)

      Applies the user-defined callback function to each element of the container $container.

      Parameters

      mixed $container Container
      callable $callable Callable to apply on each elements of $container

      Return Value

      $container

      Exceptions

      InvalidContainerException

      static boolean asort(array|ArrayObject $container, integer $flags = \SORT_REGULAR)

      Sort array values and maintain index association

      Parameters

      array|ArrayObject $container The container
      integer $flags asort function flags

      Return Value

      boolean

      Exceptions

      InvalidContainerException

      See also

      https://www.php.net/manual/en/function.asort.php

      static boolean ksort(array|ArrayObject $container, integer $flags = \SORT_REGULAR)

      Sort an array by keys

      Parameters

      array|ArrayObject $container The container
      integer $flags ksort flags

      Return Value

      boolean

      Exceptions

      InvalidContainerException

      See also

      https://www.php.net/manual/en/function.ksort.php

      static boolean natsort(array|ArrayObject $container)

      Sort an array using a "natural order" algorithm

      Parameters

      array|ArrayObject $container The container

      Return Value

      boolean

      Exceptions

      InvalidContainerException

      See also

      https://www.php.net/manual/en/function.natsort.php

      static boolean natcasesort(array|ArrayObject $container)

      Sort an array using a case insensitive "natural order" algorithm

      Parameters

      array|ArrayObject $container The container

      Return Value

      boolean

      Exceptions

      InvalidContainerException

      See also

      https://www.php.net/manual/en/function.natcasesort.php

      static boolean uasort(array|ArrayObject $container, callable $callable)

      Sort an array with a user-defined comparison function and maintain index associatio

      Parameters

      array|ArrayObject $container The container
      callable $callable Sort function Value comparison function

      Return Value

      boolean

      Exceptions

      InvalidContainerException

      See also

      https://www.php.net/manual/en/function.uasort.php

      static boolean uksort(array|ArrayObject $container, callable $callable)

      Sort an array by keys using a user-defined comparison function

      Parameters

      array|ArrayObject $container The container
      callable $callable Sort function Key comparison function

      Return Value

      boolean

      Exceptions

      InvalidContainerException

      See also

      https://www.php.net/manual/en/function.uksort.php

      static mixed[] uniqueValues(unknown $container, unknown $comparer = null)

      Removes duplicate values from an container

      Parameters

      unknown $container Input container
      unknown $comparer User-defined comparision function. The callback will receive in arguments the current candidate to insertion, one of the already inserted element and any additional argument given to the uniqueValues method.

      Return Value

      mixed[]

      Exceptions

      InvalidContainerException
      InvalidArgumentException

      static array merge()

      Merge two or more container

      Return Value

      array A new array, containing the merged content of all input containers.

      Exceptions

      InvalidContainerException

      static defaultElementComparer($a, $b)

      Parameters

      $a
      $b