Coder
Coder

Reputation: 339

Convert array in string with another array

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

Upvotes: 1

Views: 208

Answers (1)

dWinder
dWinder

Reputation: 11642

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;

Upvotes: 1

Related Questions