mergeRecursive()
mergeRecursive(array $first, array $second) : array
Merges arrays recursively and distinctly.
Parameters
array | $first | [by ref] |
array | $second | [by ref] |
Arrays related library.
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
)
}
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 |
sortByCol(array $array, string $column, integer $sort = SORT_STRING, integer $direction = SORT_ASC) : void
Sort two-dimensional array by column preserving row keys.
array | $array | [by ref] Array |
string | $column | Sort column |
integer | $sort | Sort type |
integer | $direction | Sort direction: SORT_ASC or SORT_DESC |
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.
array | $column | [by ref] Array containing integer values as order for data |