PHP Class JBZoo\Utils\Arr

Show file Open project: jbzoo/utils Class Usage Examples

Public Methods

Method Description
addEachKey ( array $array, string $prefix ) : array Add some prefix to each key
clean ( array $haystack ) : array Clean array by custom rule
cleanBeforeJson ( array $array ) : array Clean array before serialize to JSON
first ( array $array ) : mixed Returns the first element in an array.
firstKey ( array $array ) : integer | string Returns the first key in an array.
flat ( array $array, boolean $preserve_keys = true ) : array Flatten a multi-dimensional array into a one dimensional array.
getField ( array $arrayList, string $fieldName = 'id' ) : array Get one field from array of arrays (array of objects)
groupByKey ( array $arrayList, string $key = 'id' ) : array Group array by key
implode ( string $glue, array $array ) : string
in ( string $value, array $array, boolean $returnKey = false ) : mixed Check is value exists in the array
isAssoc ( $array ) : boolean Check is array is type assoc
key ( string $key, mixed $array, boolean $returnValue = false ) : mixed Check is key exists
last ( array $array ) : mixed Returns the last element in an array.
lastKey ( array $array ) : integer | string Returns the last key in an array.
map ( Closure $function, array $array ) : array Recursive array mapping
mapDeep ( array $array, string $callback, boolean $onNoScalar = false ) : array Returns an array containing all the elements of arr1 after applying the callback function to each one.
search ( array $array, mixed $search, boolean $field = false ) : boolean | mixed Searches for a given value in an array of arrays, objects and scalar values. You can optionally specify a field of the nested arrays and objects to search in.
sortByArray ( array $array, array $orderArray ) : array Sort an array by keys based on another array
toComment ( array $data ) : string Convert assoc array to comment style
unique ( array $array, boolean $keepKeys = false ) : array Remove the duplicates from an array.
unshiftAssoc ( array &$array, string $key, mixed $value ) : array Add cell to the start of assoc array
wrap ( mixed $object ) : array Wraps its argument in an array unless it is already an array

Method Details

addEachKey() public static method

Add some prefix to each key
public static addEachKey ( array $array, string $prefix ) : array
$array array
$prefix string
return array

clean() public static method

Clean array by custom rule
public static clean ( array $haystack ) : array
$haystack array
return array

cleanBeforeJson() public static method

Clean array before serialize to JSON
public static cleanBeforeJson ( array $array ) : array
$array array
return array

first() public static method

Returns the first element in an array.
public static first ( array $array ) : mixed
$array array
return mixed

firstKey() public static method

Returns the first key in an array.
public static firstKey ( array $array ) : integer | string
$array array
return integer | string

flat() public static method

Flatten a multi-dimensional array into a one dimensional array.
public static flat ( array $array, boolean $preserve_keys = true ) : array
$array array The array to flatten
$preserve_keys boolean Whether or not to preserve array keys. Keys from deeply nested arrays will overwrite keys from shallowy nested arrays
return array

getField() public static method

Get one field from array of arrays (array of objects)
public static getField ( array $arrayList, string $fieldName = 'id' ) : array
$arrayList array
$fieldName string
return array

groupByKey() public static method

Group array by key
public static groupByKey ( array $arrayList, string $key = 'id' ) : array
$arrayList array
$key string
return array

implode() public static method

public static implode ( string $glue, array $array ) : string
$glue string
$array array
return string

in() public static method

Check is value exists in the array
public static in ( string $value, array $array, boolean $returnKey = false ) : mixed
$value string
$array array
$returnKey boolean
return mixed

isAssoc() public static method

Check is array is type assoc
public static isAssoc ( $array ) : boolean
$array
return boolean

key() public static method

Check is key exists
public static key ( string $key, mixed $array, boolean $returnValue = false ) : mixed
$key string
$array mixed
$returnValue boolean
return mixed

last() public static method

Returns the last element in an array.
public static last ( array $array ) : mixed
$array array
return mixed

lastKey() public static method

Returns the last key in an array.
public static lastKey ( array $array ) : integer | string
$array array
return integer | string

map() public static method

Recursive array mapping
public static map ( Closure $function, array $array ) : array
$function Closure
$array array
return array

mapDeep() public static method

Returns an array containing all the elements of arr1 after applying the callback function to each one.
public static mapDeep ( array $array, string $callback, boolean $onNoScalar = false ) : array
$array array An array to run through the callback function
$callback string Callback function to run for each element in each array
$onNoScalar boolean Whether or not to call the callback function on nonscalar values (Objects, resources, etc)
return array

sortByArray() public static method

Sort an array by keys based on another array
public static sortByArray ( array $array, array $orderArray ) : array
$array array
$orderArray array
return array

toComment() public static method

Convert assoc array to comment style
public static toComment ( array $data ) : string
$data array
return string

unique() public static method

Remove the duplicates from an array.
public static unique ( array $array, boolean $keepKeys = false ) : array
$array array
$keepKeys boolean
return array

unshiftAssoc() public static method

Add cell to the start of assoc array
public static unshiftAssoc ( array &$array, string $key, mixed $value ) : array
$array array
$key string
$value mixed
return array

wrap() public static method

Wraps its argument in an array unless it is already an array
public static wrap ( mixed $object ) : array
$object mixed
return array