\donbidon\LibArrays

Arrays related library.

Summary

Methods
Properties
Constants
mergeRecursive()
searchByCol()
sortByCol()
adaptOrderCol()
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

mergeRecursive()

mergeRecursive(array  $first, array  $second) : array

Merges arrays recursively and distinctly.

Parameters

array $first

[by ref]

array $second

[by ref]

Returns

array

searchByCol()

searchByCol(array  $haystack, string  $key, mixed  $value, boolean  $preserveKeys = true, boolean  $strict = false) : array

Searches two-dimensional array rows for a given pair key/value and returns the corresponding rows.

Example:

use \donbidon\Utility\Arrays;

$haystack = [
    'first'  => ['id' => 1, 'value' => 'BlaH'],
    'second' => ['id' => 2, 'value' => 'Heh'],
    'third'  => ['id' => 3, 'value' => 'Wow!'],
];

$result = Arrays::searchByCol($haystack, 'id', "2");
print_r($result);

outputs

Array
(
    [first] => Array
        (
            [id] => 2
            [value] => Heh
        )
)
$result = Arrays::searchByCol($haystack, 'id', '2', false);
print_r($result);

outputs

Array
(
    [0] => Array
        (
            [id] => 2
            [value] => Heh
        )
)
$result = Arrays::searchByCol($haystack, 'id', '2', true, true);
print_r($result);

outputs

Array
(
)
$result = Arrays::searchByCol($haystack, 'value', 'Wow!');
print_r($result);

outputs

Array
(
    [fifth] => Array
        (
            [id] => 3
            [value] => Wow!
        )
)
$result = Arrays::searchByCol($haystack, 'value', '/h$/i');
print_r($result);

outputs

Array
(
    [second] => Array
        (
            [id] => 1
            [value] => BlaH
        )
    [fourth] => Array
        (
            [id] => 2
            [value] => Heh
        )
}

Parameters

array $haystack

The array

string $key

The searched key

mixed $value

The searched value, if passed as string and starts from '/' symbol, will be processed as regular expression, in this case $strict argument will be ignored

boolean $preserveKeys

Flag specifying to maintain rows index associative

boolean $strict

Flag specifying to compare values strict way

Returns

array

sortByCol()

sortByCol(array  $array, string  $column, integer  $sort = SORT_STRING, integer  $direction = SORT_ASC) : void

Sort two-dimensional array by column preserving row keys.

Parameters

array $array

[by ref] Array

string $column

Sort column

integer $sort

Sort type

integer $direction

Sort direction: SORT_ASC or SORT_DESC

adaptOrderCol()

adaptOrderCol(array  $column) : void

Adapt column containing sort order as integer values for correct using array_multisort().

Let we want to sort one array using other array as order:

$order = [2, 1];
$haystack  = ['aaa', 'bbb'];
array_multisort($order, SORT_NUMERIC, SORT_ASC, $haystack);
print_r($haystack);

outputs

Array
(
    [0] => bbb
    [1] => aaa
)

But

$order = [1, 1];
$haystack  = ['bbb', 'aaa'];
array_multisort($order, SORT_NUMERIC, SORT_ASC, $haystack);
print_r($haystack);

will change order of data and output:

Array
(
    [0] => aaa
    [1] => bbb
)

Using this class method you can prevent changing order of data.

Parameters

array $column

[by ref] Array containing integer values as order for data