Download the PHP package ajant/simple-array-library without Composer
On this page you can find all versions of the php package ajant/simple-array-library. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ajant/simple-array-library
More information about ajant/simple-array-library
Files in ajant/simple-array-library
Package simple-array-library
Short Description Simple library containing convenient array handling methods
License MIT
Homepage https://github.com/ajant/SimpleArrayLibrary
Informations about the package simple-array-library
SimpleArrayLibrary
Library containing convenient array handling methods. These are methods that I needed to create for myself during work on various projects, If you have any methods you'd like to see added, let me know: [email protected]
Requirements
You'll need: PHP version 5.4+
Quickstart
Install the latest version with composer:
Auto-load the library:
and you're ready to go.
Usage examples
For additional help, look at the tests, additional input scenarios are tested. addConfigRow
Method is intended to remove need for configuration files that need to be parsed into php arrays, since that carries significant performance penalties. It combines readability of ini file with speed of having configurations defined directly on php level. First parameter represents configuration array, second parameter represents a numerical array (in case of associative array, exception is thrown) of nested keys of the new configuration row. Third parameter represents the value that is being mapped. property, that is being added. Method returns configuration with the new row added, or throws exception if the same combination of keys has already been used.
allElementsEqual
Checks whether all elements of the array are equal and optionally if they are all equal to specified value.
castColumns
Attempts to cast specified columns of all rows of the two dimensional array to specified type. Allowed types are represented by constants: TYPE_INT, TYPE_STRING, TYPE_FLOAT, TYPE_BOOL, TYPE_ARRAY, TYPE_OBJECT. Any other type will cause exception to be thrown. User should know how type casting works in PHP as this method provides no protection from possible casting errors.
Third parameter must be a boolean, otherwise exception is thrown. If third parameter is set to true, columns defined as keys in the second parameter have to be present, otherwise exception is thrown.
countMaxDepth
Count maximum depth of the array (number of nested sub-arrays) recursively. Library contains non-recursive method doing th same thing. Recursive method is faster, but in case of very deep arrays may cause memory usage problems.
countMaxDepthIterative
Count maximum depth of the array (number of nested sub-arrays). Library contains recursive method doing th same thing. Recursive method is faster, but in case of very deep arrays may cause memory usage problems.
countMinDepth
Count minimum depth of the array (number of nested sub-arrays) recursively.
Second parameter must be an integer, or exception is thrown. This is a recursive method, second parameter is used for recursive calls and should not be used from the outside of the method.
deleteColumns
Deletes values to columns of the multi-dimensional array (is meant for two-dimensional arrays in particular, will work for three or more dimension, but will only change elements on the second level, is not recursive)
getColumns
Retrieves values of required columns out of the multi-dimensional array.
First parameter must be array of arrays (matrix) otherwise it makes no sense to search for columns & exception is thrown.
Second parameter must be an array of elements that could be used as array indexes, otherwise exception is thrown.
Third parameter must be a boolean, otherwise exception is thrown.
getRectangularDimensions
This method checks if array is "rectangular" or in other words, whether all sub-arrays have equal number of elements, recursively, and how many elements there are at each level of recursion, if it is rectangular.
hasAllKeys
Checks if all required keys are present inside an array, regardless of values.
hasAllValues
Checks if all required values are present inside an array, regardless of keys. Values must not be arrays.
hasAllValuesMultiDimensional
Checks if all required values are present inside an array, regardless of keys. Values may be arrays.
hasOnlyKeys
Checks if all required keys are present inside an array, and no other keys.
haveSameKeys
Checks if two arrays have equal keys, regardless of values.
haveSameValues
Checks if two arrays have equal values, regardless of keys.
insertSubArray
Inserts the sub-array into the array in the place to which sub-array's keys point to
Third and forth parameters must be booleans, otherwise exception is thrown. If third parameter is set to true, insertion will overwrite existing value inside the array, pointed to the sub-array's keys, if any. If third parameter is set to true, forth parameter is not used. If forth parameter is set to true nad third parameter is set to false, existing value will be left unchanged, but if both third and forth parameters were set to false and sub-array keys point to already existing value, exception will be thrown
isAssociative
Checks whether array has any associative keys.
isNumeric
Checks whether array has all numeric keys starting with zero and progressing by one for each new element.
isStructureSame
Checks whether 2 arrays have same structure (depth and keys). Values of leaf nodes are ignored, only keys are compared.
isSubArray
Checks whether array is sub-array of the other array (whether all key-value pairs of sub-array are present in array).
Third parameter must be a boolean, otherwise exception is thrown. If third parameter is set to true, strict comparison is used (===) when comparing array values, otherwise regular comparison is used (==).
selectRandomArrayElements
Select sub-array of random elements. Keys association is preserved. If required number of random elements is equal or bigger then number of original array members, entire array is returned.
Second parameter must be positive integer or string representation of the positive integer, otherwise exception is thrown.
setColumn
Sets values to columns of the multi-dimensional array (is meant for two-dimensional arrays in particular, will work for three or more dimension, but will only change elements on the second level) to the specified value
Forth and fifth parameters must be booleans, otherwise exception is thrown. If forth parameter is set to true, column will be added to the rows in which it's missing. If fifth parameter is set to true, value of the column will be overwritten in rows in which it already exists
transpose
Transpose a matrix
Turns rows into columns, and columns into rows. This is very handy when you need to reshape a dataset for a report or plot.