Library of array functions for manipulating and extracting data from arrays or 'sets' of data.
Hash
provides an improved interface, more consistent and
predictable set of features over Set
. While it lacks the spotty
support for pseudo Xpath, its more fully featured dot notation provides
similar features in a more consistent implementation.
package | Cake.Utility |
---|
_filter(array $var): boolean
array
Array to filter.
boolean
_matches(array $data,string $selector): boolean
array
Array of data to match.
string
The patterns to match.
boolean
Fitness of expression.
_matchToken(string $key,string $token): boolean
string
The key in the array being searched.
string
The token being matched.
boolean
_simpleOp(string $op,array $data,array $path,mixed $values = null): array
string
The operation to do.
array
The data to operate on.
array
The path to work on.
mixed
The values to insert when doing inserts.
array
$data.
_squash(array $data,string $key = null): array
array
The data to squash.
string
The key for the data.
array
apply(array $data,string $path,callable $function): mixed
The function will get the extracted values as the first argument.
You can easily count the results of an extract using apply(). For example to count the comments on an Article:
$count = Hash::apply($data, 'Article.Comment.{n}', 'count');
You could also use a function like array_sum
to sum the results.
$total = Hash::apply($data, '{n}.Item.price', 'array_sum');
array
The data to reduce.
string
The path to extract from $data.
callable
The function to call on each extracted value.
mixed
The results of the applied method.
check(array $data,string $path): boolean
This method uses the same path syntax as Hash::extract()
Checking for paths that could target more than one element will make sure that at least one matching element exists.
see | \Hash::extract() |
---|
array
The data to check.
string
The path to check for.
boolean
Existence of path.
combine(array $data,string $keyPath,string $valuePath = null,string $groupPath = null): array
link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::combine |
---|
array
Array from where to extract keys and values
string
A dot-separated string.
string
A dot-separated string.
string
A dot-separated string.
array
Combined array
contains(array $data,array $needle): boolean
link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::contains |
---|
array
The data to search through.
array
The values to file in $data
boolean
true if $data contains $needle, false otherwise
diff(array $data,array $compare): array
This method differs from the built-in array_diff() in that it will preserve keys and work on multi-dimensional arrays.
link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::diff |
---|
array
First value
array
Second value
array
Returns the key => value pairs that are not common in $data and $compare The expression for this function is ($data - $compare) + ($compare - ($data - $compare))
dimensions(array $data): integer
Only considers the dimension of the first element in the array.
If you have an un-even or heterogenous array, consider using Hash::maxDimensions() to get the dimensions of the array.
link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::dimensions |
---|---|
array
integer
The number of dimensions in $data
extract(array $data,string $path): array
The path expression is a dot separated expression, that can contain a set of patterns and expressions:
{n}
Matches any numeric key, or integer.{s}
Matches any string key.Foo
Matches any key with the exact same value.There are a number of attribute operators:
=
, !=
Equality.>
, <
, >=
, <=
Value comparison.=/.../
Regular expression pattern match.Given a set of User array data, from a $User->find('all')
call:
1.User.name
Get the name of the user at index 1.{n}.User.name
Get the name of every user in the set of users.{n}.User[id]
Get the name of every user with an id key.{n}.User[id>=2]
Get the name of every user with an id key greater than or equal to 2.{n}.User[username=/^paul/]
Get User elements with username matching ^paul
.array
The data to extract from.
string
The path to extract.
array
An array of the extracted values. Returns an empty array if there are no matches.
filter(array $data,callable $callback = array('self', '_filter')): array
link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::filter |
---|
array
Either an array to filter, or value when in callback
callable
A function to filter the data with. Defaults to
self::_filter()
Which strips out all non-zero empty values.
array
Filtered array
flatten(array $data,string $separator = '.'): array
link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::flatten |
---|
array
Array to flatten
string
String used to separate array key elements in a path, defaults to '.'
array
format(array $data,string $paths,string $format): array|null
Usage:
{{{ $result = Hash::format($users, array('{n}.User.id', '{n}.User.name'), '%s : %s'); }}}
The $format
string can use any format options that vsprintf()
and sprintf()
do.
link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::format |
---|---|
see | \sprintf()\Hash::extract() |
array
Source array from which to extract the data
string
An array containing one or more Hash::extract()-style key paths
string
Format string into which values will be inserted, see sprintf()
array|null
An array of strings extracted from $path
and formatted with $format
get(array $data,string|array $path): mixed
Does not support the full dot notation feature set, but is faster for simple read operations.
array
Array of data to operate on.
string|array
The path being searched for. Either a dot separated string, or an array of path segments.
mixed
The value fetched from the array, or null.
insert(array $data,string $path,array $values = null): array
array
The data to insert into.
string
The path to insert at.
array
The values to insert.
array
The data with $values inserted.
map(array $data,string $path,callable $function): array
Can be provided a path to only modify slices of the set.
array
The data to map over, and extract data out of.
string
The path to extract for mapping over.
callable
The function to call on each extracted value.
array
An array of the modified values.
maxDimensions(array $data): integer
link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::maxDimensions |
---|
array
Array to count dimensions on
integer
The maximum number of dimensions in $data
merge(array $data,mixed $merge): array
The difference between this method and the built-in ones, is that if an array key contains another array, then
Hash::merge() will behave in a recursive fashion (unlike array_merge
). But it will not act recursively for
keys that contain scalar values (unlike array_merge_recursive
).
Note: This function will work with an unlimited amount of arguments and typecasts non-array parameters into arrays.
link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::merge |
---|
array
Array to be merged
mixed
Array to merge with. The argument and all trailing arguments will be array cast when merged
array
Merged array
mergeDiff(array $data,array $compare): array
array
The data to append onto.
array
The data to compare and append onto.
array
The merged array.
nest(array $data,array $options = array()): array
children
The key name to use in the resultset for children.idPath
The path to a key that identifies each entry. Should be
compatible with Hash::extract(). Defaults to {n}.$alias.id
parentPath
The path to a key that identifies the parent of each entry.
Should be compatible with Hash::extract(). Defaults to {n}.$alias.parent_id
root
The id of the desired top-most result.see | \Hash::extract() |
---|
array
The data to nest.
array
Options are:
array
of results, nested
normalize(array $data,boolean $assoc = true): array
link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::normalize |
---|
array
List to normalize
boolean
If true, $data will be converted to an associative array.
array
numeric(array $data): boolean
link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::numeric |
---|---|
array
boolean
true if values are numeric, false otherwise
reduce(array $data,string $path,callable $function): mixed
array
The data to reduce.
string
The path to extract from $data.
callable
The function to call on each extracted value.
mixed
The reduced value.
remove(array $data,string $path): array
You can use {n}
and {s}
to remove multiple elements
from $data.
array
The data to operate on
string
A path expression to use to remove.
array
The modified array.
sort(array $data,string $path,string $dir,string $type = 'regular'): array
asc
Sort ascending.desc
Sort descending.regular
For regular sorting (don't change types)numeric
Compare values numericallystring
Compare values as stringsnatural
Compare items as strings using "natural ordering" in a human friendly way.
Will sort foo10 below foo2 as an example. Requires PHP 5.4 or greater or it will fallback to 'regular'link | http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::sort |
---|
array
An array of data to sort
string
A Set-compatible path to the array value
string
See directions above.
string
See direction types above. Defaults to 'regular'.
array
Sorted array of data