Retrieve a value from a deeply nested array using “dot” notation

JavaScript

/**
 * retrieve a value from a deeply nested array using "dot" notation
 * 
 * @param String path 
 * @param Object object 
 */
const getValueFromNestedArray = (path, object) => {
    return path.split('.').reduce( (accumulator, currentValue) => {
        return (accumulator && accumulator[currentValue]) ? accumulator[currentValue] : null
    }, object )
}

// test
console.log(getValueFromNestedArray('data.0.a', {
	code: 'code',
    data: [
        {
            a: "111"
        }
    ]
}))

PHP

function getValueFromNestedArray($arr, $path, $default = null)
{
  if (!is_array($arr) || is_null($path)) return $arr;

  if (array_key_exists($path, $arr)) {
    return $arr[$path];
  }

  if (strpos($path, '.') === false) {
    return $default;
  }

  foreach (explode('.', $path) as $segment) {
    if (is_array($arr) && array_key_exists($segment, $arr)) {
      $arr = $arr[$segment];
    } else {
      return $default;
    }
  }
  return $arr;
}

//test
var_dump(getValueFromNestedArray([
  'a' => [
    'b' => [
      'c' => time(),
      'd' => 'D'
    ]
  ]
], 'a.b.c'));