Download the PHP package belca/support-array without Composer
On this page you can find all versions of the php package belca/support-array. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package support-array
Belca\Support\Arr - PHP helper functions for handling arrays
The documentation is actual for version 1.0.
The Belca\Support\Arr
class has functions for handling arrays containing standard data (the data does not use special storage rules). Although you can use them as you like.
Some examples
Read the documentation to know about other functions.
Install
You must have PHP 7.0 and later and Composer.
Install the package. Look at the example:
Use the in your classes.
Now you can use all functions of the package.
Array functions
- Arr::concatArray
- Arr::firstExists
- Arr::firstNotEmpty
- Arr::isArrayWithIntKeys
- Arr::isFirstLastWithIntKeys
- Arr::isIntKeys
- Arr::last
- Arr::pushArray
- Arr::removeArrays
- Arr::removeEmpty
- Arr::removeEmptyRecurcive
- Arr::removeNotScalar
- Arr::removeNull
- Arr::removeNullRecurcive
- Arr::trim
- Arr::unset
- Arr::unsetByReference
Arr::concatArray
Arr::concatArray(&$source, $array, $replace = true) : void
Adds new values with new keys to the end of a source array.
If $replace is 'true' then all existing value with same keys will be replaced with new values.
Unlike the array_merge() the function does not create and it does not returns a new array, it works with the source array.
Unlike the array_merge() that adds a new array to the first array when to use integer keys, Arr::concatArray() replaces identical integer keys as if they associative keys.
The function executes $array1 += $array2
or $array2 + $array1
operations in accordance with a given $replace parameter.
Parameters:
- &$source - a source array;
- $array - an array to concat;
- $replace - replacing values. If $replace is true then all existing values that have same keys will be replaced.
Example #1: Adding new values in the array and replacing values with same keys. The array has integer keys.
First 6 values of the soure array were be replaced the new values. Also was added a new value: 12. This was happened because keys of the source array coincided with the second array.
Example #2: Adding new values in the array and replacing values with same keys. The keys of the second array are offset.
In this example we get the necessary result, the function concatenated the new values to the source array. This may sometimes be useful.
In the next example we will be to use an associative array where values that have same keys will be replaced.
Example #3: Adding new values in the array and replacing old values with same keys.
However replacing values of the source array may not always be useful. You may add only new values that are not yet in the source array.
See this example.
Example 4: Adding only new values in the array.
You might sight the function do not returns a result, it process the source array.
See also:
Arr::pushArray()
.
Arr::firstExists
Arr::firstExists(...$values): mixed
Returns the first existing value or returns 'null'.
Arr::firstNotEmpty
Arr::firstNotEmpty(...$values): mixed
Returns the first value which is not empty or returns 'null'.
Arr::isArrayWithIntKeys
Arr::isArrayWithIntKeys(array $array) : boolean
Checks whether integer keys are in an array. Uses for detecting non-associative arrays. Returns 'true' if all keys of the array are integer.
The empty array is not the integer array, because that its keys cannot be detected.
See also:
- Arr::isFirstLastWithIntKeys();
- Arr::isIntKeys().
Arr::isFirstLastWithIntKeys
Arr::isFirstLastWithIntKeys(array $array) : boolean
Checks whether the first value and the last value have integer keys. Returns 'true' if they are integer. Uses for simple detecting non-associative arrays.
The function is analogue isArrayWithIntKeys(), but keys must have some one from types: string keys or integer key.
The empty array is not the integer array, because that its keys cannot be detected.
In comparasion with Arr::isArrayWithIntKeys()
(Arr::isIntKeys()
), that may scan all values of an array, this function checks only the first and the last keys, that doing more faster.
See also:
- Arr::isIntKeys().
Arr::isIntKeys
Arr::isIntKeys(array $array) : boolean
The alias of Arr::isArrayWithIntKeys()
.
Arr::last
Arr::last(&$array) : mixed
Returns the last item of a given array. The pointer of the position of the array is saving.
Arr::pushArray
Arr::pushArray(&$array, ...$array) : void
Joins values of other arrays to the source array. Values with string keys will be replaced when they equals, values with number keys will be adjoined to the source array.
See the result. 1, 2, 3 values from $array2 were added in the source array, but the source array had these values and were added duplicates.
If you want not same values in an array, then use array_unique()
to get only unique values. The function will be apply to all keys, even to string keys, where different keys may keep same values.
See also:
- Arr::concatArray().
Arr::removeArrays
Arr::removeArrays($array, $resetIndex = false) : array
Removes nested arrays (subarrays) from an array. If $resetIndex is 'true' then resets keys of the array.
Parameters:
- $array - an array;
- $resetIndex - the reset of keys of the array. If $resetIndex is true then resets keys of the array.
Example 1: Removing inner arrays.
In the example in the array were removed all arrays and kept other values.
Sometimes you need to reset keys of an array as shown below.
Example 2: Remove inner arrays and resetting keys the array.
Arr::removeEmpty
Arr::removeEmpty(array $array) : array
Removes each values of an array where the empty value. Uses the function 'empty'. Keys of the array are saving.
Arr::removeEmptyRecurcive
Arr::removeEmptyRecurcive(array $array, boolean $resetIndex = true) : array
Recursively removes the empty values of a multidimensional array. If the function takes not array then it returns an unchanged value.
Parameters:
- $array - some array;
- $resetIndex - the reset of keys of the array. If $resetIndex is true then resets integer keys of each array, including keys set manually.
Example #1. Recursive removing empty values and resetting keys of arrays.
In the example you see that resetting keys were only in arrays with integer keys. So the basic keys of the array and the keys of the $arrar['a3']
value was not changed, and the others keys were reset.
Example 2. Recursive removing empty values without resetting keys
In the example the keys were not changed, because $resetIndex was set as false.
See also:
- Arr::removeNullRecurcive().
Arr::removeNotScalar
Arr::removeNotScalar(array $array) : array
Removes each values of an array with the value is 'null' or other values that are not scalar values. Scalar values are those containing an integer, float, string or boolean.
Arr::removeNull
Arr::removeNull(array $array) : array
Removes each values of an array where a value is 'null'. Uses the function 'is_null'. Keys of the array are saving.
Arr::removeNullRecurcive
Arr::removeNullRecurcive(array $array, boolean $resetIndex = true) : array
Recursively removes the null values of a multidimensional array.
Parameters:
- $array - some array;
- $resetIndex - the reset of keys of the array. If $resetIndex is true then resets integer keys of each array, including keys set manually. If an array has one or more non-integer key then keys are saving. If the function takes not array then it returns an unchanged value.
Example 1: Removing values with 'null' and resetting keys.
In the example performing resetting keys of the arrays where all keys of an array are integer. So the keys of $array[2]
were not changed, but the index of the value was changed.
Example 2: Removing values with 'null' and without resetting keys.
See also:
- Arr::removeEmptyRecurcive().
Arr::trim
Arr::trim(array $array) : array
Trims string values of an array. Keys of the array are saving. The function does not handle nested arrays.
Arr::unset
Arr::unset($array, ...$indexes) : array
Removes values of an array by given keys without changing keys of the array. Returns a new array.
This function may be useful when you need to remove from an array unknown values by knowing their indexes. The same effect may be get using array_filter()
or unset()
, but this function are more readable and compact.
Arr::unsetByReference
Arr::unsetByReference(array &$array, ...$keys): void
Removes values of an array by given keys. The function do not return a result.
Example 1: Removing values using simple keys
Example 2: Removing values using keys in arrays
See also:
- Arr::unset().