2

I want a function that will return TRUE or FALSE based on if a given key exists in a multidimensional array in PHP.

I haven't been able to figure out a recursive function to perform this action.

A sample of what this could do:

$array = array(
    'key 1' => array(
        'key 1.1' => array()
        'key 1.2' => array()
    ),
    'key 2' => array(
        'key 2.1' => array(
            'key 2.1.1' => array()
        )
        'key 2.2' => array()
    )
);

multi_array_key_exists('key 1', $array); // return TRUE

multi_array_key_exists('key 2.1.1', $array); // return TRUE

multi_array_key_exists('key 3', $array); // return FALSE
1
  • Where are your attempts at writing multi_array_key_exists? Commented Nov 14, 2013 at 19:06

3 Answers 3

7

This is where a recursive function comes in handy.

function multi_array_key_exists($key, array $array): bool
{
    if (array_key_exists($key, $array)) {
        return true;
    } else {
        foreach ($array as $nested) {
            if (is_array($nested) && multi_array_key_exists($key, $nested))
                return true;
        }
    }
    return false;
}

Note that this can take some time (in long nested arrays), it might be better to flatten first, since you are only interested in whether the key exists or not.

Sign up to request clarification or add additional context in comments.

Comments

2

Maybe too late but here's a solution I came up with:

function multi_array_key_exists(array $path, array $array): bool
{
    if (empty($path)) {
        return false;
    }
    foreach ($path as $key) {
        if (isset($array[$key]) || array_key_exists($key, $array)) {
            $array = $array[$key];
            continue;
        }

        return false;
    }

    return true;
}

$testMe = ['my' => ['path' => ['exists' => true]]];
multi_array_key_exists(['my', 'path', 'exists'], $testMe);

I does not need costly recursive calls and is slightly faster (~15% on my setup).

1 Comment

It's never too late for a useful solution! 👍
0

If you want to find the endpoint key [Knowing only the last key] (But that key could exist in multiple sub elements)

    public function has(array $array, string $pointer): bool
    {
        $iterator = new RecursiveIteratorIterator(
            new RecursiveArrayIterator($array), 
            RecursiveIteratorIterator::SELF_FIRST
        );

        foreach($iterator as $key => $value){
            if($key === $pointer) {
                return true;
            }
        }

        return false;
    }

If you want to to check a key in a sequence like... users.admins.is_big_boss

    public function has(array $array, string $pointer): bool
    {
        $depth = explode('.', $pointer);
        $base = $array;

        for($i=0; $i < count($depth); $i++) {

            if(!isset($base[$depth[$i]])) {
                return false;
            }

            $base = $base[$depth[$i]];
        }

        return true;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.