NArray
in package
Enhanced array manipulation utility class for the Neuron framework.
This class provides a comprehensive set of array manipulation methods with an object-oriented approach, offering convenient operations for array processing, filtering, transformation, and analysis. It serves as a modern replacement for the static ArrayHelper class with additional functionality and consistent behavior.
Key features:
- Object-oriented array wrapper with property hooks
- Safe array element access with default values
- Comprehensive transformation methods (map, filter, reduce)
- Array operations (merge, diff, intersect, unique)
- Extraction and slicing operations
- Sorting and ordering capabilities
- Collection analysis methods
- Fluent interface for method chaining
- Modern PHP 8.4+ syntax with property hooks
Tags
Table of Contents
Properties
Methods
- __construct() : mixed
- NArray constructor.
- avg() : int|float|null
- Calculate the average of all numeric values in the array.
- chunk() : NArray
- Split the array into chunks.
- contains() : bool
- Check if the array contains a specific value, optionally at a specific key.
- count() : int
- Get the number of elements in the array.
- each() : NArray
- Execute a callback for each element in the array.
- filter() : NArray
- Filter the array using a callback function.
- find() : mixed
- Find the first element matching a callback function.
- findBy() : mixed
- Find the first element with a specific key-value pair.
- first() : mixed
- Get the first element of the array.
- getElement() : mixed
- Get an element from the array with optional default value.
- hasKey() : bool
- Check if a key exists in the array.
- implode() : string
- Join array elements with a string.
- indexOf() : int|string|false
- Find the index of an element in the array.
- isEmpty() : bool
- Check if the array is empty.
- isNotEmpty() : bool
- Check if the array is not empty.
- keys() : NArray
- Get all keys from the array.
- last() : mixed
- Get the last element of the array.
- map() : NArray
- Apply a callback function to each element and return a new NArray.
- max() : mixed
- Find the maximum value in the array.
- merge() : NArray
- Merge this array with another array or NArray.
- min() : mixed
- Find the minimum value in the array.
- pluck() : NArray
- Extract a specific key from each element (for arrays of arrays/objects).
- reduce() : mixed
- Reduce the array to a single value using a callback function.
- remove() : NArray
- Remove an element from the array by value.
- reverse() : NArray
- Reverse the array and return a new NArray.
- slice() : NArray
- Extract a slice of the array.
- sort() : NArray
- Sort the array and return a new NArray.
- sortKeys() : NArray
- Sort the array by keys and return a new NArray.
- sum() : int|float
- Calculate the sum of all numeric values in the array.
- toArray() : array<string|int, mixed>
- Get the raw array value.
- toJson() : string
- Convert the array to JSON string.
- unique() : NArray
- Get unique values from the array.
- values() : NArray
- Get all values from the array.
- where() : NArray
- Filter elements by a specific key-value pair.
Properties
$value
public
array<string|int, mixed>
$value
$_value
private
array<string|int, mixed>
$_value
Methods
__construct()
NArray constructor.
public
__construct([array<string|int, mixed> $array = [] ]) : mixed
Parameters
- $array : array<string|int, mixed> = []
avg()
Calculate the average of all numeric values in the array.
public
avg() : int|float|null
Return values
int|float|null —The average or null if no numeric values
chunk()
Split the array into chunks.
public
chunk(int $size[, bool $preserveKeys = false ]) : NArray
Parameters
- $size : int
-
The size of each chunk
- $preserveKeys : bool = false
-
Whether to preserve keys
Return values
NArray —A new NArray containing arrays of chunks
contains()
Check if the array contains a specific value, optionally at a specific key.
public
contains(mixed $value[, string|int|null $key = null ]) : bool
Parameters
- $value : mixed
-
The value to search for
- $key : string|int|null = null
-
Optional key to check for the value
Return values
bool —True if the value is found, false otherwise
count()
Get the number of elements in the array.
public
count() : int
Return values
int —The number of elements
each()
Execute a callback for each element in the array.
public
each(callable $callback) : NArray
Parameters
- $callback : callable
-
The callback function to execute
Return values
NArray —Returns self for method chaining
filter()
Filter the array using a callback function.
public
filter(callable $callback) : NArray
Parameters
- $callback : callable
-
The callback function for filtering
Return values
NArray —A new NArray with filtered values
find()
Find the first element matching a callback function.
public
find(callable $callback[, mixed $default = null ]) : mixed
Parameters
- $callback : callable
-
The callback function for matching
- $default : mixed = null
-
Default value if no match found
Return values
mixed —The first matching element or default value
findBy()
Find the first element with a specific key-value pair.
public
findBy(string|int $key, mixed $value[, mixed $default = null ]) : mixed
Parameters
- $key : string|int
-
The key to check
- $value : mixed
-
The value to match
- $default : mixed = null
-
Default value if no match found
Return values
mixed —The first matching element or default value
first()
Get the first element of the array.
public
first([mixed $default = null ]) : mixed
Parameters
- $default : mixed = null
-
Default value if array is empty
Return values
mixed —The first element or default value
getElement()
Get an element from the array with optional default value.
public
getElement(string|int $key[, mixed $default = null ]) : mixed
Parameters
- $key : string|int
-
The key to retrieve
- $default : mixed = null
-
Default value if key doesn't exist
Return values
mixed —The value at the key or the default value
hasKey()
Check if a key exists in the array.
public
hasKey(string|int $key) : bool
Parameters
- $key : string|int
-
The key to check for
Return values
bool —True if the key exists, false otherwise
implode()
Join array elements with a string.
public
implode(string $glue) : string
Parameters
- $glue : string
-
The string to join elements with
Return values
string —The joined string
indexOf()
Find the index of an element in the array.
public
indexOf(mixed $item) : int|string|false
Parameters
- $item : mixed
-
The item to search for
Return values
int|string|false —The key of the item or false if not found
isEmpty()
Check if the array is empty.
public
isEmpty() : bool
Return values
bool —True if the array is empty, false otherwise
isNotEmpty()
Check if the array is not empty.
public
isNotEmpty() : bool
Return values
bool —True if the array has elements, false otherwise
keys()
Get all keys from the array.
public
keys() : NArray
Return values
NArray —A new NArray containing the keys
last()
Get the last element of the array.
public
last([mixed $default = null ]) : mixed
Parameters
- $default : mixed = null
-
Default value if array is empty
Return values
mixed —The last element or default value
map()
Apply a callback function to each element and return a new NArray.
public
map(callable $callback) : NArray
Parameters
- $callback : callable
-
The callback function to apply
Return values
NArray —A new NArray with transformed values
max()
Find the maximum value in the array.
public
max() : mixed
Return values
mixed —The maximum value or null if empty
merge()
Merge this array with another array or NArray.
public
merge(array<string|int, mixed>|NArray $array) : NArray
Parameters
- $array : array<string|int, mixed>|NArray
-
The array to merge with
Return values
NArray —A new NArray with merged values
min()
Find the minimum value in the array.
public
min() : mixed
Return values
mixed —The minimum value or null if empty
pluck()
Extract a specific key from each element (for arrays of arrays/objects).
public
pluck(string|int $key) : NArray
Parameters
- $key : string|int
-
The key to extract
Return values
NArray —A new NArray with extracted values
reduce()
Reduce the array to a single value using a callback function.
public
reduce(callable $callback[, mixed $initial = null ]) : mixed
Parameters
- $callback : callable
-
The callback function for reduction
- $initial : mixed = null
-
Initial value for the reduction
Return values
mixed —The reduced value
remove()
Remove an element from the array by value.
public
remove(mixed $item) : NArray
Parameters
- $item : mixed
-
The item to remove
Return values
NArray —Returns self for method chaining
reverse()
Reverse the array and return a new NArray.
public
reverse([bool $preserveKeys = false ]) : NArray
Parameters
- $preserveKeys : bool = false
-
Whether to preserve keys
Return values
NArray —A new NArray with reversed values
slice()
Extract a slice of the array.
public
slice(int $offset[, int|null $length = null ][, bool $preserveKeys = false ]) : NArray
Parameters
- $offset : int
-
Starting position
- $length : int|null = null
-
Number of elements to extract
- $preserveKeys : bool = false
-
Whether to preserve keys
Return values
NArray —A new NArray with the slice
sort()
Sort the array and return a new NArray.
public
sort([int $sortFlags = SORT_REGULAR ]) : NArray
Parameters
- $sortFlags : int = SORT_REGULAR
-
Sort flags (SORT_REGULAR, SORT_NUMERIC, etc.)
Return values
NArray —A new NArray with sorted values
sortKeys()
Sort the array by keys and return a new NArray.
public
sortKeys([int $sortFlags = SORT_REGULAR ]) : NArray
Parameters
- $sortFlags : int = SORT_REGULAR
-
Sort flags (SORT_REGULAR, SORT_NUMERIC, etc.)
Return values
NArray —A new NArray with sorted keys
sum()
Calculate the sum of all numeric values in the array.
public
sum() : int|float
Return values
int|float —The sum of all numeric values
toArray()
Get the raw array value.
public
toArray() : array<string|int, mixed>
Return values
array<string|int, mixed> —The internal array
toJson()
Convert the array to JSON string.
public
toJson([int $flags = 0 ]) : string
Parameters
- $flags : int = 0
-
JSON encoding flags
Return values
string —JSON representation of the array
unique()
Get unique values from the array.
public
unique() : NArray
Return values
NArray —A new NArray with unique values
values()
Get all values from the array.
public
values() : NArray
Return values
NArray —A new NArray containing the values
where()
Filter elements by a specific key-value pair.
public
where(string|int $key, mixed $value) : NArray
Parameters
- $key : string|int
-
The key to check
- $value : mixed
-
The value to match
Return values
NArray —A new NArray with matching elements