Skip to main content
added 1 character in body
Source Link
mickmackusa
  • 8.8k
  • 1
  • 17
  • 31

I recommend avoiding declaring single-use variables. I also recommend throwing an exception when a broken path is passed into your function. Better variable naming will help others to instantly understand your code.

Code: (Demo)

function get_array_deep_value(array $array, array $levelKeys) {
    foreach ($levelKeys as $index => $key) {
        if (!key_exists($key, $array)) {
            throw new Exception("key $key not found at level index $index");
        }
        $array = $array[$key];
    }
    return $array;
}

$myArray = [
    'one' => [
        'two' => [
            'three' => [
                4
            ]
        ]
    ]
];

$pathArrays = [
    ['one', 'two', 'three'],
    ['one', 'two'],
    ['one'],
    ['two']
];

foreach ($pathArrays as $pathArray) {
    try {
        var_export(get_array_deep_value($myArray, $pathArray));
    } catch (Exception$eException $e) {
        echo $e->getMessage();
    }
    echo "\n---\n";
}

I recommend avoiding declaring single-use variables. I also recommend throwing an exception when a broken path is passed into your function. Better variable naming will help others to instantly understand your code.

Code: (Demo)

function get_array_deep_value(array $array, array $levelKeys) {
    foreach ($levelKeys as $index => $key) {
        if (!key_exists($key, $array)) {
            throw new Exception("key $key not found at level index $index");
        }
        $array = $array[$key];
    }
    return $array;
}

$myArray = [
    'one' => [
        'two' => [
            'three' => [
                4
            ]
        ]
    ]
];

$pathArrays = [
    ['one', 'two', 'three'],
    ['one', 'two'],
    ['one'],
    ['two']
];

foreach ($pathArrays as $pathArray) {
    try {
        var_export(get_array_deep_value($myArray, $pathArray));
    } catch (Exception$e) {
        echo $e->getMessage();
    }
    echo "\n---\n";
}

I recommend avoiding declaring single-use variables. I also recommend throwing an exception when a broken path is passed into your function. Better variable naming will help others to instantly understand your code.

Code: (Demo)

function get_array_deep_value(array $array, array $levelKeys) {
    foreach ($levelKeys as $index => $key) {
        if (!key_exists($key, $array)) {
            throw new Exception("key $key not found at level index $index");
        }
        $array = $array[$key];
    }
    return $array;
}

$myArray = [
    'one' => [
        'two' => [
            'three' => [
                4
            ]
        ]
    ]
];

$pathArrays = [
    ['one', 'two', 'three'],
    ['one', 'two'],
    ['one'],
    ['two']
];

foreach ($pathArrays as $pathArray) {
    try {
        var_export(get_array_deep_value($myArray, $pathArray));
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    echo "\n---\n";
}
Source Link
mickmackusa
  • 8.8k
  • 1
  • 17
  • 31

I recommend avoiding declaring single-use variables. I also recommend throwing an exception when a broken path is passed into your function. Better variable naming will help others to instantly understand your code.

Code: (Demo)

function get_array_deep_value(array $array, array $levelKeys) {
    foreach ($levelKeys as $index => $key) {
        if (!key_exists($key, $array)) {
            throw new Exception("key $key not found at level index $index");
        }
        $array = $array[$key];
    }
    return $array;
}

$myArray = [
    'one' => [
        'two' => [
            'three' => [
                4
            ]
        ]
    ]
];

$pathArrays = [
    ['one', 'two', 'three'],
    ['one', 'two'],
    ['one'],
    ['two']
];

foreach ($pathArrays as $pathArray) {
    try {
        var_export(get_array_deep_value($myArray, $pathArray));
    } catch (Exception$e) {
        echo $e->getMessage();
    }
    echo "\n---\n";
}