dynamical index array

- Posted in PHP by

You can pass an array of indexes to this function below. So if you would like to get $some_array[0]['client_name']['third_level_index'] then you can do this:

function get_array_value(array $array, array $indexes)
{
    if (count($array) == 0 || count($indexes) == 0) {
        return false;
    }

    $index = array_shift($indexes);
    if(!array_key_exists($index, $array)){
        return false;
    }

    $value = $array[$index];
    if (count($indexes) == 0) {
        return $value;
    }

    if(!is_array($value)) {
        return false;
    }

    return get_array_value($value, $indexes);
}

$some_array = array(/* nested array */);

$indexes = array(0, 'client_name', 'third_level_index');

$value = get_array_value($some_array, $indexes);

_https://stackoverflow.com/questions/5458241/php-dynamic-array-index-name

Tags: