\Nano4\UtilsArry

Some useful utility functions that work on Arrays.

The easiest way to use this is: use Nano4\Utils\Arry; Then just call the functions like: Arry::swap($array, 2, 4);

You can make an object instance, but it's not needed.

Summary

Methods
Properties
Constants
swap()
rename_key()
cartesian_product()
subsets()
merge_into_each()
powerset()
powerset2()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

swap()

swap(array  $array, mixed  $pos1, mixed  $pos2) 

Swap the positions of elements in an array.

This operates directly on the array, and does not return anything.

Parameters

array $array

The array to operate on.

mixed $pos1

First position or key to swap.

mixed $pos2

Second position or key to swap.

rename_key()

rename_key(array  $array, string  $curname, string  $newname, boolean  $overwrite = False) 

Rename an array key.

This operates directly on the array. It returns true on success, and false on failure.

Parameters

array $array

The array to operate on.

string $curname

Current key name.

string $newname

New key name.

boolean $overwrite

Optional, overwrite existing keys.

cartesian_product()

cartesian_product(array  $arrays) : array

Generate a Cartesian product from a set of arrays.

Taken from http://www.theserverpages.com/php/manual/en/ref.array.php Reformatted to fit with Nano.

Parameters

array $arrays

An array of arrays to get the product of.

Returns

array —

An array of arrays representing the product.

subsets()

subsets(array  $array, integer  $size) 

Generate a set of subsets of a fixed size.

Based on example from: stackoverflow.com/questions/7327318/power-set-elements-of-a-certain-length

Parameters

array $array

Array to find subsets of.

integer $size

Size of subsets we want.

merge_into_each()

merge_into_each(mixed  $x, array  $arrays) : array

Merge an item into a set of arrays.

A part of subsets(), taken from same example.

Parameters

mixed $x

Item to merge into arrays.

array $arrays

Array of arrays to merge $x into.

Returns

array —

A copy of original array, with merged data.

powerset()

powerset(array  $array) : array

Generate a powerset.

Generate an array of arrays representing the powerset of elements from the original array.

Based on code from: http://bohuco.net/blog/2008/11/php-arrays-power-set-and-all-permutations/

Parameters

array $array

The input array to generate powerset from.

Returns

array —

An array of arrays representing the powerset.

powerset2()

powerset2(  $in,   $minLength = 1) 

Another powerset algorithm.

Found in a few places on the net.

Parameters

$in
$minLength