/ Gists / PHP nested value in multi array with own separator
On gists

PHP nested value in multi array with own separator

PHP Libs PHP

fn.php Raw #

<?php

function getNestedValue($array, $path, $separator = '.') {
    $keys = explode($separator, $path);
    
    foreach ($keys as $key) {
        if (isset($array[$key])) {
            $array = $array[$key];
        } else {
            return null; // nebo hodnotu podle vašich potřeb
        }
    }
    
    return $array;
}


$x = [
    'a' => [
        'b' => [
            'c' => 'FINAL C ... ;)'
        ],
    ],
];

echo getNestedValue($x, 'a.b.c');