1

I have a two array first is:

$array1 = ['settings:rules:key','settings:scrum:way:other'];

I have explode $array1:

$temp_array = explode(":",$array1);

Now I have another array:

$array2 = [settings] => Array
        (  [rules] => Array
                (
                    [0] => Array
                        (
                            [key] => 
                            [showValueField] => 1
                     ) 
                )

something like this.

I need to access second array with key given in first array like:

$array2['settings']['rules']['key']

I have to get this keys from first array after explode

6
  • 1
    You have a problem in that $array2['settings']['rules'] doesn't have an element ['key'], it has an element [0]['key'] Commented Feb 2, 2019 at 6:39
  • i need to get keys from first array and access second array. can you write a code Commented Feb 2, 2019 at 6:41
  • Your second array keys don't follow the values in the first array. Your first array value would need to be 'settings:rules:0:key' to match your second array. Commented Feb 2, 2019 at 6:42
  • 1
    You also can't explode an array you'd need to explode each value Commented Feb 2, 2019 at 6:42
  • can you give a perfect solution. i'll get keys in array to access another array. i there any way to do it. Commented Feb 2, 2019 at 6:46

1 Answer 1

1

You can do it with this kind of loop:

function getVal($path, $arr) {
    $current = $arr[array_shift($path)];
    while (count($path)) {
        $key = array_shift($path);
        if (!is_array($current) || !isset($current[$key]))
            return false; // protect against non-existing keys
        $current = $current[$key];
    }
    return $current;
}

//example used:
$arr = array("settings" => array("rules" => array("key" => "AAA")));
echo getVal(explode(":",'settings:rules:key'), $arr) . PHP_EOL;
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.