Recommend this page to a friend! 
Classes of Christian Vigh  >  PHP Array Helpers  >  README.md  >  Download 

DownloadINTRODUCTIONThe ArrayHelpers class contains static methods that perform various and completely different operations on arrays. It is to be considered as some kind of swissknife and provides the following features :
REFERENCEArrayHelpers::CombinationsOf ( $array, $max_results = 10000 ) ;Takes an array containing values and nested arrays (no more than one level of nesting is authorized) and generates all the possible combinations, each nested array providing alternatives during the generation.
An example will be better than a long description ; suppose that you have the following array :
You want to generate all the possible combinations given the above input, ie, a result such as this one :
The CombinationsOf method will do the work and will return you the following array :
[ [ 'a', 1, 2, 'x' ] [ 'b', 1, 2, 'x' ] [ 'a', 1, 2, 'y' ] [ 'b', 1, 2, 'y' ] [ 'a', 1, 2, 'z' ] [ 'b', 1, 2, 'z' ] ]
For example :
Of course, if you array does not contain nested arrays, the return value will be an array containing only one element, the value you supplied for the $array parameter. For example :
will return :
The $max\_results parameter is here to limit the number of items that will be returned by the function, just in case you provided array data which leads to exponential combinations. Note that :
ArrayHelpers::CrossReference ( $keys, $array, &$missing = [], &$extra = [], $case_sensitive = true ) ;Performs a crossreference between two arrays. The first array contains key names, and the second one is either an associative array (with key names) or a regular array (with indexes). The function checks that each element in $array (identified either by its key or its numeric index) has a correspondance in $keys. On output :
The $case\_sensitive parameter specifies if comparisons on array keys should be casesensitive or not. This parameter is useless if the $array parameter is a nonassociative array. The function returns true if the contents of the $keys array are strictly the same as the array keys of the $array parameter, or false otherwise. InArray ( $array, $value, $case_insensitive = true )Checks that the specified value is in the specified array. By default, comparisons are not casesensitive. This function can be used if you do not have the extension tha contains the iin_array() function (however, I am unable to tell you what is the name of this extension...). $result = ArrayHelpers::MergeAssoc ( $array [, ...] ) ;The MergeAssoc()function can be considered as similar to the PHParray\_merge()* function ; however, it operates on array keys rather than on array values. For example, the following :
will produce the following output :
while the array_merge() function will give :
$result = ArrayHelpers::MergeAssocRecursive ( $array [, ...] ) ;The MergeAssocRecursive() function is similar to the PHP array\_merge\_recursive() function. However, like the MergeAssoc() function, it operates on array keys rather than on array values. For example, the following :
will give the following result :
while the array\_merge\_recursive() function will give :
ArrayHelpers::MultiSort ( &$array, $criterias, $case_sensitive = true ) ;The MultiSort() function is intended to sort arrays whose values are associative arrays containing the same keys. It allows for specifying multiple sort criterias. The $array parameter can either be an array of associative arrays, or an array of objects. Each element in the $criteria array specifies a sort criteria, which must reference an array item key in the $array parameter (if array items are themselves associative arrays) or a property (if array items are objects). Each value in the $criterias array must be a boolean value that indicates the sort direction for the associated array key or object property name : true for sorting in ascending order, false for descending order. On output, the original contents of $array will be replaced with their sorted contents, while the function will return the number of comparisons performed. The following examples sorts the $array parameter on two elements, 'prop1' (in ascending order) and 'prop2' (in descending order) :
It will output :
This also works on arrays of objects, using the same value for the $criterias array :
$index = ArrayHelpers::SortedFind ( $array, $value ) ;Returns the index of the element in $array whose value is equal to the one specified for the $value parameter. This function uses a dichotomic search and assumes that the supplied array is already sorted. The complexity of this function is thus O(log2(n)), where n is the number of elements contained in the array (for arrays containing 1 million elements, the number of comparisons will be log2(1000000), so at most 20 comparisons will be required to find the desired element). It returns boolean false if the specified value was not found, or the index of the specified value in $array. The following functions are also available :
$result = ToRanges ( $array ) ;Extracts ranges of values from an array containing integer elements, and returns an array of 2elements arrays, which contain in tur the low and upperbound of the range. The following example :
will display :
Note that the order of elements in $array has no importance ; you could specify as well :
$result = ToRangeString ( $array, $range_separator = '', $item_separator = ', ' ) ;Converts an array of integers to a string representing ranges. The following for example :
will display :
The parameters are the following :
ArrayHelper::Utf8Encode ( &$array ) ; ArrayHelper::Utf8Decode ( &$array ) ;Recursively encodes/decodes an array. Decoded data is put in the supplied $array argument. 