2

I need to get all the keys from this array. I tried with a recursive call but only get first element keys. I want all the array keys to store into the array with level. means keys[0] = array(540,198). below is the array that I want to traverse and get all the keys. I had worked on more than 1 day but not get anything please help to fix this.

(
    [ch] => Array
        (
            [540] => Array
                (
                    [194] => Array
                        (
                            [16] => Array
                                (
                                    [144] => Array
                                        (
                                        )

                                    [145] => Array
                                        (
                                        )

                                    [146] => Array
                                        (
                                        )

                                    [147] => Array
                                        (
                                        )

                                    [148] => Array
                                        (
                                        )

                                    [195] => Array
                                        (
                                        )

                                    [199] => Array
                                        (
                                        )

                                    [149] => Array
                                        (
                                        )

                                    [200] => Array
                                        (
                                        )

                                    [150] => Array
                                        (
                                        )

                                    [444] => Array
                                        (
                                        )

                                    [151] => Array
                                        (
                                        )

                                    [445] => Array
                                        (
                                        )

                                    [446] => Array
                                        (
                                        )

                                    [152] => Array
                                        (
                                        )

                                )

                        )

                )

            [198] => Array
                (
                    [194] => Array
                        (
                            [16] => Array
                                (
                                    [144] => Array
                                        (
                                        )

                                    [145] => Array
                                        (
                                        )

                                    [146] => Array
                                        (
                                        )

                                    [147] => Array
                                        (
                                        )

                                    [148] => Array
                                        (
                                        )

                                    [195] => Array
                                        (
                                        )

                                    [199] => Array
                                        (
                                        )

                                    [149] => Array
                                        (
                                        )

                                    [200] => Array
                                        (
                                        )

                                    [150] => Array
                                        (
                                        )

                                    [444] => Array
                                        (
                                        )

                                    [151] => Array
                                        (
                                        )

                                    [445] => Array
                                        (
                                        )

                                    [446] => Array
                                        (
                                        )

                                    [152] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)
$this->checkProductConfiguraions($configuraion,$count,$levelKeys);
function checkProductConfiguraions($configuraion,$count,$levelKeys) {
    foreach ($configuraions as $configuraion) {
        if (is_array($configuraion)) {
                $parentKeys = array_keys($configuraion);
                if(isset($levelKeys[$count])){
                    $levelKeys[$count] = array_unique(array_merge($levelKeys[$count],$parentKeys));
                } else {
                    $levelKeys[$count] = $parentKeys;
                }

                $count++;
                $levelKeys[$count] = array();
                for($i=0;$i<count($parentKeys);$i++){
                    $levelKeys[$count] = array_unique(array_merge($levelKeys[$count],array_keys($configuraion[$parentKeys[$i]])));
                    if(is_array($configuraion)){
                        $levelKeys = $this->checkProductConfiguraions($configuraion,$count,$levelKeys);
                    }
                }
        }
    }
}
2
  • Post your code. Commented Feb 25, 2020 at 6:59
  • @SimoneRossaini I added code. have a look and do necessary modifications. Commented Feb 25, 2020 at 8:17

1 Answer 1

1
function check($configurations, $count = 0, $levelKeys = array())
{
    foreach ($configurations as $k => $configuration) {
        if (!array_key_exists($count, $levelKeys) || 
            !in_array($k, $levelKeys[$count], true)) {
            $levelKeys[$count][] = $k;
        }
        if (!empty($configuration)) {
            $levelKeys = $this->check($configuration, $count + 1, $levelKeys);
        }
    }
    return $levelKeys;
}

DEMO

During the first round I fill the $levelKeys variable with the first key.

In the first IF I use in_array() and array_key_exist() to avoid duplicate values.

If the array I'm analyzing has a nested array I call the check() function passing: nested array, level I have reached, levelKeys.

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

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.