Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wildcard matching for has() and get() #74

Open
hassankhan opened this issue Mar 8, 2016 · 3 comments
Open

Wildcard matching for has() and get() #74

hassankhan opened this issue Mar 8, 2016 · 3 comments

Comments

@hassankhan
Copy link
Owner

array(
    'env1' => [
        'servers' => array(
            'host1',
            'host2',
            'host3'
        ),
    ],
    'env2' => [
        'servers' => array(
            'host4',
            'host5',
            'host6'
        ),
    ],
    'env3' => [
        'servers' => array(
            'host7',
            'host8',
            'host9'
        ),
    ],
);

$servers = Config::has('*.servers');

The example is contrived, but I've needed something similar in a project already. Not quite sure how we're gonna figure it out though 😕

@DavidePastore
Copy link
Collaborator

Yes, this one could be a solution for the given problem. Do you think that every * should be only for one position? What I mean is that it won't work for:

array(
  'env1' => array(
    'foo1' => [
        'servers' => array(
            'host1',
            'host2',
            'host3',
        ),
    ],
  ),
  'env2' => array(
    'foo2' => [
        'servers' => array(
            'host4',
            'host5',
            'host6',
        ),
    ],
  ),
  'env3' => array(
    'foo3' => [
        'servers' => array(
            'host7',
            'host8',
            'host9',
        ),
    ],
  ),
);

@hassankhan
Copy link
Owner Author

I think it should also work for both our examples. If we add something like this, we should be sure to document it well. Any thoughts on how to approach this code-wise?

@DavidePastore
Copy link
Collaborator

RecursiveArrayIterator will help us.
Take a look to this solution, it's exactly what we want:

/**
 * Recursive find in the given $array and $needle.
 * @param $array The input array.
 * @param $needle The needle to find.
 * @return Returns all the values find by the given key.
 */
function recursiveFind(array $array, $needle)
{
    $iterator  = new RecursiveArrayIterator($array);
    $recursive = new RecursiveIteratorIterator($iterator,
                         RecursiveIteratorIterator::SELF_FIRST);
    $results = array();
    foreach ($recursive as $key => $value) {
        if ($key === $needle) {
            array_push($results, $value);
        }
    }
    return $results;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants