Overview
  • Namespace
  • Class
  • Tree

Namespaces

  • PHP
  • Yiinitializr
    • Cli
    • Clio
    • Composer
    • Helpers

Classes

  • ArrayX
  • Config
  • Initializer

Class ArrayX

ArrayX provides a set of useful functions

Namespace: Yiinitializr\Helpers
Package: Yiinitializr\helpers
Author: Antonio Ramirez <amigo.cobos@gmail.com>
Since: 1.0
Located at Helpers/ArrayX.php

Methods summary

public static mixed
# get( array $array, string $key, mixed $default = null )

Get an item from an array using "dot" notation.

Get an item from an array using "dot" notation.

// Get the $array['user']['name'] value from the array
$name = ArrayX::get($array, 'user.name');

// Return a default from if the specified item doesn't exist
$name = ArrayX::get($array, 'user.name', 'Taylor');

Parameters

$array
array
$array
$key
string
$key
$default
mixed
$default

Returns

mixed
public static mixed|null
# pop( mixed & $array, mixed $key, null $default = null )

Removes an item from the given options and returns the value.

Removes an item from the given options and returns the value.

If no key is found, then default value will be returned.

Parameters

$array
mixed
$array
$key
mixed
$key
$default
null
$default

Returns

mixed|null
public static
# set( array & $array, string $key, mixed $value )

Set an array item to a given value using "dot" notation.

Set an array item to a given value using "dot" notation.

If no key is given to the method, the entire array will be replaced.

// Set the $array['user']['name'] value on the array
ArrayX::set($array, 'user.name', 'Taylor');

// Set the $array['user']['name']['first'] value on the array
ArrayX::set($array, 'user.name.first', 'Michael');

Parameters

$array
array
$array
$key
string
$key
$value
mixed
$value
public static
# forget( array & $array, string $key )

Remove an array item from a given array using "dot" notation.

Remove an array item from a given array using "dot" notation.

// Remove the $array['user']['name'] item from the array
ArrayX::forget($array, 'user.name');

// Remove the $array['user']['name']['first'] item from the array
ArrayX::forget($array, 'user.name.first');

Parameters

$array
array
$array
$key
string
$key
public static mixed
# first( array $array, Closure $callback, mixed $default = null )

Return the first element in an array which passes a given truth test.

Return the first element in an array which passes a given truth test.

// Return the first array element that equals "Taylor"
$value = ArrayX::first($array, function($k, $v) {return $v == 'Taylor';});

// Return a default value if no matching element is found
$value = ArrayX::first($array, function($k, $v) {return $v == 'Taylor'}, 'Default');

Parameters

$array
array
$array
$callback
Closure
$callback
$default
mixed
$default

Returns

mixed
public static array
# stripSlashes( array $array )

Recursively remove slashes from array keys and values.

Recursively remove slashes from array keys and values.

Parameters

$array
array
$array

Returns

array
public static array
# divide( array $array )

Divide an array into two arrays. One with keys and the other with values.

Divide an array into two arrays. One with keys and the other with values.

Parameters

$array
array
$array

Returns

array
public static array
# pluck( array $array, string $key )

Pluck an array of values from an array.

Pluck an array of values from an array.

Parameters

$array
array
$array
$key
string
$key

Returns

array
public static array
# only( array $array, array $keys )

Get a subset of the items from the given array.

Get a subset of the items from the given array.

Parameters

$array
array
$array
$keys
array
$keys

Returns

array
public static array
# except( array $array, array $keys )

Get all of the given array except for a specified array of items.

Get all of the given array except for a specified array of items.

Parameters

$array
array
$array
$keys
array
$keys

Returns

array
public static mixed
# head( array $array )

Return the first element of an array.

Return the first element of an array.

This is simply a convenient wrapper around the "reset" method.

Parameters

$array
array
$array

Returns

mixed
public static array
# merge( array $a, array $b )

Merges two or more arrays into one recursively. If each array has an element with the same string key value, the latter will overwrite the former (different from array_merge_recursive). Recursive merging will be conducted if both arrays have an element of array type and are having the same key. For integer-keyed elements, the elements from the latter array will be appended to the former array.

Merges two or more arrays into one recursively. If each array has an element with the same string key value, the latter will overwrite the former (different from array_merge_recursive). Recursive merging will be conducted if both arrays have an element of array type and are having the same key. For integer-keyed elements, the elements from the latter array will be appended to the former array.

Parameters

$a
array
$a array to be merged to
$b
array
$b array to be merged from. You can specifiy additional arrays via third argument, fourth argument etc.

Returns

array
the merged array (the original arrays are not changed.)
public static boolean|mixed|string
# deepSearch( array $array, string $search, boolean $field = FALSE )

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.

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.

Credits to Util.php

Parameters

$array
array
$array The array to search
$search
string
$search The value to search for
$field
boolean
$field The field to search in, if not specified all fields will be searched

Returns

boolean|mixed|string
False on failure or the array key on

Link

https://github.com/brandonwamboldt/utilphp/blob/master/util.php
public static array
# deepMap( array $array, mixed $callback, boolean $on_nonscalar = FALSE )

Returns an array containing all the elements of arr1 after applying the callback function to each one.

Returns an array containing all the elements of arr1 after applying the callback function to each one.

Credits to Util.php

Parameters

$array
array
$array an array to run through the callback function
$callback
mixed
$callback Callback function to run for each element in each array
$on_nonscalar
boolean
$on_nonscalar whether or not to call the callback function on nonscalar values (objects, resr, etc)

Returns

array

Link

https://github.com/brandonwamboldt/utilphp/blob/master/util.php
public static mixed
# value( mixed $value )

Return the value of the given item.

Return the value of the given item.

If the given item is a Closure the result of the Closure will be returned.

Parameters

$value
mixed
$value

Returns

mixed

Magic methods summary

API documentation generated by ApiGen 2.8.0