PHP Class Arrayzy\AbstractArray

Author: Victor Bocharsky ([email protected])
Inheritance: implements ArrayAcces\ArrayAccess, implements Arrayzy\Interfaces\ConvertibleInterface, implements Countabl\Countable, implements Arrayzy\Interfaces\DebuggableInterface, implements IteratorAggregat\IteratorAggregate, implements Arrayzy\Interfaces\ModifiableInterface, implements Arrayzy\Interfaces\SortableInterface, implements Arrayzy\Interfaces\TraversableInterface, use trait Arrayzy\Traits\ConvertibleTrait, use trait Arrayzy\Traits\DebuggableTrait, use trait Arrayzy\Traits\ModifiableTrait, use trait Arrayzy\Traits\TraversableTrait
Show file Open project: bocharsky-bw/arrayzy Class Usage Examples

Protected Properties

Property Type Description
$elements array

Public Methods

Method Description
__construct ( array $elements = [] ) Construct new instance
chunk ( integer $size, boolean $preserveKeys = false ) : AbstractArray Create a chunked version of current array.
clear ( ) : AbstractArray Clear the current array.
combine ( array $array ) : AbstractArray Create an array using the current array as keys and the other array as values.
contains ( mixed $element ) : boolean Check if the given value exists in the array.
containsKey ( mixed $key ) : boolean Check if the given key/index exists in the array.
count ( ) : integer Returns the number of values in the array.
create ( array $elements = [] ) : AbstractArray Create a new instance.
createClone ( ) : AbstractArray Clone current instance to new instance.
createFromJson ( string $json, integer $options, integer $depth = 512 ) : AbstractArray Decode a JSON string to new instance.
createFromObject ( ArrayAccess $elements ) : AbstractArray Create a new instance filled with values from an object implementing ArrayAccess.
createFromString ( string $string, string $separator ) : AbstractArray Explode a string to new instance by specified separator.
createWithRange ( mixed $low, mixed $high, integer $step = 1 ) : AbstractArray Create a new instance containing a range of elements.
diff ( array $array ) : AbstractArray Compute the current array values which not present in the given one.
except ( array $keys ) : AbstractArray Return slice of an array except given keys.
exists ( callable $func ) : boolean Find the given value in An array using a closure
filter ( callable $func ) : AbstractArray Filter the current array for elements satisfying the predicate $func.
find ( callable $func ) : mixed Returns the first occurrence of a value that satisfies the predicate $func.
flip ( ) : AbstractArray Exchanges all keys of current array with their associated values.
getIterator ( ) : Traversable Create an iterator over this array.
getKeys ( ) : array Return an array all the keys of this array.
getRandom ( ) : mixed Pick a random value out of this array.
getRandomKey ( ) : mixed Pick a random key/index from the keys of this array.
getRandomKeys ( integer $number ) : mixed Pick a given number of random keys/indexes out of this array.
getRandomValues ( integer $number ) : array Pick a given number of random values with non-duplicate indexes out of the array.
getValues ( ) : mixed Return an array of all values from this array numerically indexed.
indexOf ( mixed $element ) : mixed Alias of search() method. Search for a given element and return the index of its first occurrence.
intersect ( array $array ) : AbstractArray Compute the current array values which present in the given one.
intersectAssoc ( array $array ) : AbstractArray Compute the current array values with additional index check which present in the given one.
intersectKey ( array $array ) : AbstractArray Compute the current array using keys for comparison which present in the given one.
isAssoc ( ) : boolean Check whether array is associative or not.
isEmpty ( ) : boolean Check whether the array is empty or not.
isNumeric ( ) : boolean Check whether array is numeric or not.
map ( callable $func ) : AbstractArray Apply the given function to the every element of the current array, collecting the results.
merge ( array $array, boolean $recursively = false ) : AbstractArray Merge the current array with the provided one. The latter array is overwriting.
offsetExists ( mixed $offset ) : boolean Whether an offset exists.
offsetGet ( mixed $offset ) : mixed Retrieve the current offset or null.
offsetSet ( mixed $offset, mixed $value ) Set an offset for this array.
offsetUnset ( mixed $offset ) Remove a present offset.
only ( array $keys ) : AbstractArray Return slice of an array with just a given keys.
pad ( integer $size, mixed $value ) : AbstractArray Pad the current array to the specified size with a given value.
reduce ( callable $func, mixed | null $initial = null ) : mixed Reduce the array to a single value iteratively combining all values using $func.
reindex ( ) : AbstractArray Create a numerically re-indexed array based on the current array.
replace ( array $array, boolean $recursively = false ) : AbstractArray Replace values in the current array with values in the given one that have the same key.
reverse ( boolean $preserveKeys = false ) : AbstractArray Reverse the values order of the current array.
search ( mixed $element ) : mixed Search for a given element and return the index of its first occurrence.
shuffle ( ) : AbstractArray Randomize elements order of the current array.
slice ( integer $offset, integer | null $length = null, boolean $preserveKeys = false ) : AbstractArray Extract a slice of the current array.
unique ( integer | null $sortFlags = null ) : AbstractArray Remove duplicate values from the current array.
walk ( callable $func, boolean $recursively = false ) : AbstractArray Apply the given function to the every element of the current array, discarding the results.

Private Methods

Method Description
checkType ( string $type ) : boolean Check if an array keys are in a given variable type.

Method Details

__construct() public method

Construct new instance
public __construct ( array $elements = [] )
$elements array

chunk() abstract public method

Create a chunked version of current array.
abstract public chunk ( integer $size, boolean $preserveKeys = false ) : AbstractArray
$size integer Size of each chunk
$preserveKeys boolean Whether array keys are preserved or no
return AbstractArray An array of chunks from the current array

clear() abstract public method

Clear the current array.
abstract public clear ( ) : AbstractArray
return AbstractArray The current empty array

combine() abstract public method

Create an array using the current array as keys and the other array as values.
abstract public combine ( array $array ) : AbstractArray
$array array Values array
return AbstractArray An array with values from the other array

contains() public method

Check if the given value exists in the array.
public contains ( mixed $element ) : boolean
$element mixed Value to search for
return boolean Returns true if the given value exists in the array, false otherwise

containsKey() public method

Check if the given key/index exists in the array.
public containsKey ( mixed $key ) : boolean
$key mixed Key/index to search for
return boolean Returns true if the given key/index exists in the array, false otherwise

count() public method

Returns the number of values in the array.
public count ( ) : integer
return integer total number of values

create() public static method

Create a new instance.
public static create ( array $elements = [] ) : AbstractArray
$elements array
return AbstractArray Returns created instance

createClone() public method

Clone current instance to new instance.
Deprecation: Should be removed
public createClone ( ) : AbstractArray
return AbstractArray Shallow copy of $this

createFromJson() public static method

Decode a JSON string to new instance.
public static createFromJson ( string $json, integer $options, integer $depth = 512 ) : AbstractArray
$json string The JSON string being decoded
$options integer Bitmask of JSON decode options
$depth integer Specified recursion depth
return AbstractArray The created array

createFromObject() public static method

Create a new instance filled with values from an object implementing ArrayAccess.
public static createFromObject ( ArrayAccess $elements ) : AbstractArray
$elements ArrayAccess Object that implements ArrayAccess
return AbstractArray Returns created instance

createFromString() public static method

Explode a string to new instance by specified separator.
public static createFromString ( string $string, string $separator ) : AbstractArray
$string string Converted string
$separator string Element's separator
return AbstractArray The created array

createWithRange() public static method

Create a new instance containing a range of elements.
public static createWithRange ( mixed $low, mixed $high, integer $step = 1 ) : AbstractArray
$low mixed First value of the sequence
$high mixed The sequence is ended upon reaching the end value
$step integer Used as the increment between elements in the sequence
return AbstractArray The created array

diff() abstract public method

Compute the current array values which not present in the given one.
abstract public diff ( array $array ) : AbstractArray
$array array Array for diff
return AbstractArray An array containing all the entries from this array that are not present in $array

except() public method

Return slice of an array except given keys.
public except ( array $keys ) : AbstractArray
$keys array List of keys to exclude
return AbstractArray The created array

exists() public method

Find the given value in An array using a closure
public exists ( callable $func ) : boolean
$func callable
return boolean Returns true if the given value is found, false otherwise

filter() abstract public method

Filter the current array for elements satisfying the predicate $func.
abstract public filter ( callable $func ) : AbstractArray
$func callable
return AbstractArray An array with only element satisfying $func

find() public method

Returns the first occurrence of a value that satisfies the predicate $func.
public find ( callable $func ) : mixed
$func callable
return mixed The first occurrence found

flip() abstract public method

Exchanges all keys of current array with their associated values.
abstract public flip ( ) : AbstractArray
return AbstractArray An array with flipped elements

getIterator() public method

Create an iterator over this array.
public getIterator ( ) : Traversable
return Traversable An instance of an object implementing Iterator

getKeys() public method

Return an array all the keys of this array.
public getKeys ( ) : array
return array An array of all keys

getRandom() public method

Pick a random value out of this array.
public getRandom ( ) : mixed
return mixed Random value of array

getRandomKey() public method

Pick a random key/index from the keys of this array.
public getRandomKey ( ) : mixed
return mixed Random key/index of array

getRandomKeys() public method

Pick a given number of random keys/indexes out of this array.
public getRandomKeys ( integer $number ) : mixed
$number integer The number of keys/indexes (should be <= $this->count())
return mixed Random keys or key of array

getRandomValues() public method

Pick a given number of random values with non-duplicate indexes out of the array.
public getRandomValues ( integer $number ) : array
$number integer The number of values (should be > 1 and < $this->count())
return array Random values of array

getValues() public method

Return an array of all values from this array numerically indexed.
public getValues ( ) : mixed
return mixed An array of all values

indexOf() public method

Alias of search() method. Search for a given element and return the index of its first occurrence.
public indexOf ( mixed $element ) : mixed
$element mixed Value to search for
return mixed The corresponding key/index

intersect() abstract public method

Compute the current array values which present in the given one.
abstract public intersect ( array $array ) : AbstractArray
$array array Array for intersect
return AbstractArray An array with containing all the entries from this array that are present in $array

intersectAssoc() abstract public method

Compute the current array values with additional index check which present in the given one.
abstract public intersectAssoc ( array $array ) : AbstractArray
$array array Array for intersect
return AbstractArray An array with containing all the entries from this array that are present in $array. Note that the keys are also used in the comparison unlike in intersect().

intersectKey() abstract public method

Compute the current array using keys for comparison which present in the given one.
abstract public intersectKey ( array $array ) : AbstractArray
$array array Array for intersect
return AbstractArray An array with containing all the entries from this array which have keys that are present in $array.

isAssoc() public method

Check whether array is associative or not.
public isAssoc ( ) : boolean
return boolean Returns true if associative, false otherwise

isEmpty() public method

Check whether the array is empty or not.
public isEmpty ( ) : boolean
return boolean Returns true if empty, false otherwise

isNumeric() public method

Check whether array is numeric or not.
public isNumeric ( ) : boolean
return boolean Returns true if numeric, false otherwise

map() abstract public method

Apply the given function to the every element of the current array, collecting the results.
abstract public map ( callable $func ) : AbstractArray
$func callable
return AbstractArray An array with modified elements

merge() abstract public method

Merge the current array with the provided one. The latter array is overwriting.
abstract public merge ( array $array, boolean $recursively = false ) : AbstractArray
$array array Array to merge with (overwrites)
$recursively boolean Whether array will be merged recursively or no
return AbstractArray An array with the keys/values from $array added

offsetExists() public method

Whether an offset exists.
public offsetExists ( mixed $offset ) : boolean
$offset mixed An offset to check for.
return boolean true on success or false on failure.

offsetGet() public method

Retrieve the current offset or null.
public offsetGet ( mixed $offset ) : mixed
$offset mixed The offset to retrieve.
return mixed Can return all value types.

offsetSet() public method

Set an offset for this array.
public offsetSet ( mixed $offset, mixed $value )
$offset mixed The offset to assign the value to.
$value mixed The value to set.

offsetUnset() public method

Remove a present offset.
public offsetUnset ( mixed $offset )
$offset mixed The offset to unset.

only() public method

Return slice of an array with just a given keys.
public only ( array $keys ) : AbstractArray
$keys array List of keys to return
return AbstractArray The created array

pad() abstract public method

Pad the current array to the specified size with a given value.
abstract public pad ( integer $size, mixed $value ) : AbstractArray
$size integer Size of the result array
$value mixed Empty value by default
return AbstractArray An array padded to $size with $value

reduce() public method

Reduce the array to a single value iteratively combining all values using $func.
public reduce ( callable $func, mixed | null $initial = null ) : mixed
$func callable callback ($carry, $item) -> next $carry
$initial mixed | null starting value of the $carry
return mixed Final value of $carry

reindex() abstract public method

Create a numerically re-indexed array based on the current array.
abstract public reindex ( ) : AbstractArray
return AbstractArray An array with re-indexed elements

replace() abstract public method

Replace values in the current array with values in the given one that have the same key.
abstract public replace ( array $array, boolean $recursively = false ) : AbstractArray
$array array Array of replacing values
$recursively boolean Whether array will be replaced recursively or no
return AbstractArray An array with the same keys but new values

reverse() abstract public method

Reverse the values order of the current array.
abstract public reverse ( boolean $preserveKeys = false ) : AbstractArray
$preserveKeys boolean Whether array keys are preserved or no
return AbstractArray An array with the order of the elements reversed

shuffle() abstract public method

Randomize elements order of the current array.
abstract public shuffle ( ) : AbstractArray
return AbstractArray An array with the shuffled elements order

slice() abstract public method

Extract a slice of the current array.
abstract public slice ( integer $offset, integer | null $length = null, boolean $preserveKeys = false ) : AbstractArray
$offset integer Slice begin index
$length integer | null Length of the slice
$preserveKeys boolean Whether array keys are preserved or no
return AbstractArray A new array, which is slice of the current array with specified $length

unique() abstract public method

Remove duplicate values from the current array.
abstract public unique ( integer | null $sortFlags = null ) : AbstractArray
$sortFlags integer | null
return AbstractArray An array with only unique elements

walk() abstract public method

Apply the given function to the every element of the current array, discarding the results.
abstract public walk ( callable $func, boolean $recursively = false ) : AbstractArray
$func callable
$recursively boolean Whether array will be walked recursively or no
return AbstractArray An array with modified elements

Property Details

$elements protected property

protected array $elements
return array