Make your array usage quite easy and fast.
You have to provide you array to constructor and after that you can do a lot of operations on haystack via class methods.
// Some array to be processed.
$array = [
[0, 1, 2, 3, 5],
'fruits' => ['apple', 'orange'],
'persons' => [
'john_doe' => [
'name' => 'John Doe',
'cars' => ['bmw', 'audi'],
'isActive' => true,
'comment' => 'some description',
],
'mike_shepard' => [
'name' => 'Mike Shepard',
'cars' => [],
'isActive' => false,
'comment' => null
],
]
];
// Making FlexArray.
$array = new FlexArray($array);
- get
- getInteger
- getFloat
- getString
- getBoolean
- getAny
- getAll
- getKeys
- getAllBut
- getAllIntegers
- getAllStrings
- getAllBooleans
- getAllCleaned
- getFirst
- getFirstKey
- getLast
- getLastKey
- getByIndex
- getKeyOfIndex
- getUpTo
- implodeAll
- implode
- implodeKeys
- count
- keyOf
- keysOf
- indexOf
- indexesOf
- binarySearch
- toJson
- touch
- findValues
- isEmpty
- keyExists
- hasKeys
- hasAnyKey
- hasValues
- hasAnyValue
- inCount
- assertEqualsByKey
- assertAnyEqualsByKey
get(int|string $key, mixed $default = null): mixed|null
Returns value from haystack by $key
or $default
(null on default) on not set.
getInteger(int|string $key, mixed $default = null): ?int
Returns value from haystack by $key
cast to integer or $default
(null on default) on not set.
getFloat(int|string $key, mixed $default = null): ?float
Returns value from haystack by $key
cast to float or $default
(null on default) on not set.
getString(int|string $key, mixed $default = null): ?string
Returns value from haystack by $key
cast to string or $default
(null on default) on not set.
getBoolean(int|string $key, mixed $default = null): ?bool
Returns value from haystack by $key
cast to boolean or $default
(null on default) on not set.
getAny(int|string ...$keys): mixed|null
Returns value first found by $keys
or null
on not found.
getAll(): array
Returns haystack as array.
getKeys(): array
Return keys of the haystack.
getAllBut(int|string ...$keys): array
Returns list of haystack values but provided in $keys
.
getAllIntegers(): array
Returns list of integer values indexed by its keys.
getAllStrings(): array
Returns list of string values indexed by its keys.
getAllBooleans(): array
Returns list of boolean values indexed by its keys.
getAllClean(): array
Returns the haystack of not real empty values.
getFirst(): mixed
Returns the first element.
getFirstKey(): int|string|null
Returns first key of the haystack or null on empty haystack.
getLast(): mixed
Returns the last element.
getLastKey(): int|string|null
Returns last key of the haystack or null on empty haystack.
getByIndex(int $index): mixed
Returns value by index or nul if $index
is not in haystack length. Pass negative number for reverse search.
getKeyOfIndex(int $index): int|string|null
Returns key of integer index in the haystack or nul if $index
is not in haystack length. Pass negative number for reverse search.
getUpTo(int $index): array
Return values up to $index
. Pass negative number for reverse search.
implodeAll(string $separator, bool $associative = false): string
Returns array values imploded to string recursively.
Pass true
in $associative
to represent in indexed by its keys.
implode(string $separator, bool $associative = false): string
Returns only scalar values in the haystack as string.
Pass true
in $associative
to represent in indexed by its keys.
implodeKeys(string $separator): string
Returns haystack keys as string.
count(): int
Returns the haystack length.
keyOf(int|string $key): int|string|null
Returns key of value in the haystack or null
on found.
keysOf(int|string $key): array
Returns list of keys for haystack values.
indexOf(mixed $needle): int|null
Returns integer index of value or null
on not found.
indexesOf(mixed $needle): array
Returns list of integer indexes of provided values.
binarySearch(int $needle): int|null
Binary search method for integer value in haystack. Sorts the haystack due the process. Returns the integer index of value on found or null on not.
toJson(): string
Represents haystack in JSON string content. Trows Exception
on decode error.
touch(int|string ...$keys): array
Returns only scalar values in the haystack as string.
Pass true
in $associative
to represent in indexed by its keys.
findValues(mixed ...$values): array
Returns list of found values associated with found keys in haystack.
set(int|string $key, mixed $value): self
Sets value by $key
.
prepend(mixed $value): self
Adds value to haystack to the top of haystack.
append(mixed $value): self
Adds value to haystack in the end.
delete(int|string $key): self
Deletes value by $key
.
deleteOnFound(mixed ...$values): self
Remove haystack elements by values if exists.
deleteFirst(): self
Deletes the first element.
deleteLast(): self
Deletes the last element.
deleteByIndex(int|string $key): self
Deletes element by index.
deleteAll(): self
Delete all values and sets empty array to haystack.
flip(): self
Flips the haystack.
merge(array ...$arrays): self
Merges the elements of one or more arrays together.
unique(int $flags = SORT_STRING): self
Returns haystack keys as string.
Available flags
- SORT_REGULAR - compare items normally (don't change types)
- SORT_NUMERIC - compare items numerically
- SORT_STRING - compare items as strings
- SORT_LOCALE_STRING - compare items as strings, based on the current locale.
sort(int $flags = SORT_REGULAR): self
Sort haystack.
Available flags
- SORT_REGULAR - compare items normally; the details are described in the comparison operators section
- SORT_NUMERIC - compare items numerically
- SORT_STRING - compare items as strings
- SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using
setlocale()
- SORT_NATURAL - compare items as strings using "natural ordering" like
natsort()
- SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
rsort(int $flags = SORT_REGULAR): self
Sort haystack in reverse order.
Available flags
- SORT_REGULAR - compare items normally; the details are described in the comparison operators section
- SORT_NUMERIC - compare items numerically
- SORT_STRING - compare items as strings
- SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using
setlocale()
- SORT_NATURAL - compare items as strings using "natural ordering" like
natsort()
- SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
ksort(int $flags = SORT_REGULAR): self
Sort the haystack by keys.
Available flags
- SORT_REGULAR - compare items normally; the details are described in the comparison operators section
- SORT_NUMERIC - compare items numerically
- SORT_STRING - compare items as strings
- SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using
setlocale()
- SORT_NATURAL - compare items as strings using "natural ordering" like
natsort()
- SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
krsort(int $flags = SORT_REGULAR): self
Sort the haystack by keys in reverse order.
Available flags
- SORT_REGULAR - compare items normally; the details are described in the comparison operators section
- SORT_NUMERIC - compare items numerically
- SORT_STRING - compare items as strings
- SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using
setlocale()
- SORT_NATURAL - compare items as strings using "natural ordering" like
natsort()
- SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
asort(int $flags = SORT_REGULAR): self
Sort an array and maintain index association.
Available flags
- SORT_REGULAR - compare items normally; the details are described in the comparison operators section
- SORT_NUMERIC - compare items numerically
- SORT_STRING - compare items as strings
- SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using
setlocale()
- SORT_NATURAL - compare items as strings using "natural ordering" like
natsort()
- SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
arsort(int $flags = SORT_REGULAR): self
Sort an array and maintain index association in reverse order.
Available flags
- SORT_REGULAR - compare items normally; the details are described in the comparison operators section
- SORT_NUMERIC - compare items numerically
- SORT_STRING - compare items as strings
- SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using
setlocale()
- SORT_NATURAL - compare items as strings using "natural ordering" like
natsort()
- SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively
clean(): self
Cleans real empty values in the haystack.
createBy(int|string $key): self
Creates and returns inner collection (new static()
) by $key
if indexed value in the haystack is array. Otherwise, collection with empty haystack returns. Now, inner array is available to processed with all described methods.
createByFirst(): self
Creates class object by first element.
createByLast(): self
Creates class object by last element.
createByIndex(): self
Creates class object by element located by index. Pass negative number to get it in reverse order.
isEmpty(int|string $key): bool
Defines if value by $key
exists and is not empty
keyExists(int|string $key): bool
Defines if the given key or index exists in the haystack even nullable.
hasKeys(mixed ...$keys): bool
Defines if all provided keys exists.
hasAnyKey(mixed ...$keys): bool
Defines if the haystack has any value (even nullable) indexed by keys provided.
hasValues(mixed ...$values): bool
Defines if the haystack has any value provided.
hasAnyValue(mixed ...$values): bool
Defines if the haystack has any value provided.
inCount(int ...$count): bool
Defines if the haystack length in provided value.
assertEqualsByKey(int|string $key, mixed ...$values): bool
Asserts if each value identically equals the value in the haystack by key.
assertAnyEqualsByKey(int|string $key, mixed ...$values): bool
Asserts if any value identically equals the value in the haystack by key.