You are here

public static function ArrayHelper::getNestedValue in Helper 7

2 calls to ArrayHelper::getNestedValue()
ArrayHelper::extractNestedValuesToArray in lib/ArrayHelper.php
ArrayHelper::filterByNestedValue in lib/ArrayHelper.php

File

lib/ArrayHelper.php, line 5

Class

ArrayHelper

Code

public static function getNestedValue($item, array $parents, &$key_exists = NULL) {
  $ref = $item;
  foreach ($parents as $parent) {
    if (is_array($ref) && array_key_exists($parent, $ref)) {
      $ref =& $ref[$parent];
    }
    elseif (is_object($ref) && property_exists($ref, $parent)) {
      $ref = $ref->{$parent};
    }
    elseif (is_object($ref) && method_exists($ref, '__get') && $ref
      ->__get($parent) !== NULL) {

      // Support objects that override the __get magic method.
      // This also doesn't support if $ref->$parent exists but is set to NULL.
      $ref = $ref->{$parent};
    }
    else {
      $key_exists = FALSE;
      return NULL;
    }
  }
  $key_exists = TRUE;
  return $ref;
}