com\setasign\SetaFramework\Helper

ArrayHelper Class ArrayHelper

File: /SetaFramework/src/Helper/ArrayHelper.php

Class hierarchy

Summary

Static Methods

distribute()

public static ArrayHelper::distribute (
array $array,
callable $callback,
int $expectedResults = 2,
bool $preserveKey = false
): array

Distribute an array by a callback into x-groups (x = $expectedResults). The callback must have the following signature: function($value, $key): int (The returned integer must be within 0 and ($expectedResult - 1))

Parameters
$array : array
 
$callback : callable

function($value, $key): int

$expectedResults : int

Defines into how many groups you want to distribute the array.

$preserveKey : bool

If true the original key will be kept otherwise the key will be lost and forgotten.

ensureArray()

public static ArrayHelper::ensureArray (
mixed $value
): array

Ensures that the value is an array.

If the value isn't an array it will be packed into an array.

Parameters
$value : mixed
 

ensureTravesable()

public static ArrayHelper::ensureTravesable (
mixed $value,
iterable $default = array ( )
): \iterable

Ensures that the value is travesable.

If the value isn't travesable $default is returned.

Parameters
$value : mixed
 
$default : iterable
 

first()

public static ArrayHelper::first (
array $array,
mixed $default = null
): ?mixed

Returns the first item of an array

Parameters
$array : array
 
$default : mixed

Will be returned if $array is empty

flatten()

public static ArrayHelper::flatten (
array $array,
float|int $depth = \INF
): array

Flattens a multi-dimensional array

Parameters
$array : array
 
$depth : float|int
 

groupBy()

public static ArrayHelper::groupBy (
array $array,
callable $groupBy,
bool $errorOnDuplicate = true
): array
Parameters
$array : array
 
$groupBy : callable
 
$errorOnDuplicate : bool
 

isArrayOfClass()

public static ArrayHelper::isArrayOfClass (
array $array,
string $objectType
): bool
Parameters
$array : array
 
$objectType : string
 

isArrayOfInt()

public static ArrayHelper::isArrayOfInt (
array $array
): bool
Parameters
$array : array
 

isArrayOfString()

public static ArrayHelper::isArrayOfString (
array $array
): bool
Parameters
$array : array
 

last()

public static ArrayHelper::last (
array $array,
mixed $default = null
): ?mixed

Returns the last item of an array

Parameters
$array : array
 
$default : mixed

Will be returned if $array is empty

limit()

public static ArrayHelper::limit (
array $array,
int $limit,
int $offset,
&$hasMore
): array
Parameters
$array : array
 
$limit : int
 
$offset : int
 
$hasMore
 

naturalLanguageImplode()

public static ArrayHelper::naturalLanguageImplode (
string $glue,
string $lastGlue,
array $pieces
): string
Parameters
$glue : string
 
$lastGlue : string
 
$pieces : array
 

prepend()

public static ArrayHelper::prepend (
array $array,
mixed $value,
?mixed $key = null
): array

Push an item onto the beginning of an array

Parameters
$array : array
 
$value : mixed
 
$key : ?mixed
 

pull()

public static ArrayHelper::pull (
array &$array,
mixed $key,
mixed $default = null
): ?mixed

Pulls an item out of an array

Parameters
$array : array
 
$key : mixed
 
$default : mixed

Will be returned if the item doesn't exist

pushWithKey()

public static ArrayHelper::pushWithKey (
array $array,
mixed $key,
mixed $value
): array

Simple helper method to insert a value into an array

Parameters
$array : array
 
$key : mixed
 
$value : mixed
 

random()

public static ArrayHelper::random (
array $array,
?int $amount = null,
bool $preserveKeys = false
): array

Get one or a specified number of random values from an array.

Parameters
$array : array
 
$amount : ?int
 
$preserveKeys : bool
 
Exceptions

Throws \InvalidArgumentException

shuffleAssoc()

public static ArrayHelper::shuffleAssoc (
array &$array
): bool

Shuffles $array but preserves keys.

Parameters
$array : array
 

striInArray()

public static ArrayHelper::striInArray (
string $string,
array $array
): bool|string

Checks whether the string is in the array (case-insensitive).

Parameters
$string : string
 
$array : array