Skip to content

New method: ArrDots::search

Compare
Choose a tag to compare
@pdscopes pdscopes released this 30 Aug 08:45
· 26 commits to main since this release

ArrDots::search is a new method that, given a key with possible wildcards, will return an associative array of all matching dot notations to their values. For example:

$data = [
    'field0' => 'field0-value',
    'array0' => [1, 2, 3, 4],
    'array1' => [
        ['item0' => 'item0-value'],
        ['item0' => 'item1-value'],
    ],
    'array2' => [
        ['sub-array0' => [1, 2, 3, 4]],
        ['sub-array0' => [1, 2, 3, 4]],
    ],
    'array3' => [
        ['sub-array1' => [['item1' => 'item2-value'], ['item1' => 'item3-value']]],
        ['sub-array1' => [['item1' => 'item4-value']]],
    ],
];

// Simple
$results = ArrDots::search($data, 'field0');
$results === [
    'field0' => 'field0-value
];

// End with wildcard
$results = ArrDots::search($data, 'array0.*', '*');
$results === [
    'array0.0' => 1,
    'array0.1' => 2,
    'array0.2' => 3,
    'array0.3' => 4,
];

// Single wildcard inside
$results = ArrDots::search($data, 'array2.*.sub-array0', '*');
$results === [
    'array2.0.sub-array0' => [1, 2, 3, 4],
    'array2.1.sub-array0' => [1, 2, 3, 4],
];

// Multiple wildcard
$results = ArrDots::search($data, 'array3.*.sub-array1.*.item1', '*');
$results === [
    'array3.0.sub-array1.0.item1' => 'item2-value',
    'array3.0.sub-array1.1.item1' => 'item3-value',
    'array3.1.sub-array1.0.item1' => 'item4-value',
];