Skip to content

Commit

Permalink
Add utils for recursively finding a key in an array when the depth of…
Browse files Browse the repository at this point in the history
… the key is unknown
  • Loading branch information
eileenmcnaughton committed May 23, 2018
1 parent 7f874ed commit 70cf3be
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions CRM/Utils/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -1152,4 +1152,101 @@ public static function findInTree($search, $tree, $field = 'id') {
return NULL;
}

/**
* Check if a key isset which may be several layers deep.
*
* This is a helper for when the calling function does not know how many layers deep the
* path array is so cannot easily check.
*
* @param array $array
* @param array $path
* @return bool
* @throws \CRM_Core_Exception
*/
public static function recursiveIsset($array, $path) {
$count = count($path);
switch ($count) {
case 0;
return FALSE;

case 1;
return isset($array[$path[0]]);

case 2;
return isset($array[$path[0]][$path[1]]);

case 3;
return isset($array[$path[0]][$path[1]][$path[2]]);

case 4;
return isset($array[$path[0]][$path[1]][$path[2]][$path[3]]);

case 5;
return isset($array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]]);

case 6;
return isset($array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]][$path[5]]);

case 7;
return isset($array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]][$path[5]][$path[6]]);

case 8;
return isset($array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]][$path[5]][$path[6]][$path[7]]);

default:
throw new CRM_Core_Exception('More levels in array than are supported');
}
}

/**
* Check if a key isset which may be several layers deep.
*
* This is a helper for when the calling function does not know how many layers deep the
* path array is so cannot easily check.
*
* @param array $array
* @param array $path
* An array of keys - e.g [0, 'bob', 8] where we want to check if $array[0]['bob'][8]
* @param mixed $default
* Value to return if not found.
* @return bool
* @throws \CRM_Core_Exception
*/
public static function recursiveGetValue($array, $path, $default = FALSE) {
$count = count($path);
switch ($count) {
case 0;
return $default;

case 1;
return isset($array[$path[0]]) ? $array[$path[0]] : $default;

case 2;
return isset($array[$path[0]][$path[1]]) ? $array[$path[0]][$path[1]] : $default;

case 3;
return isset($array[$path[0]][$path[1]][$path[2]]) ? $array[$path[0]][$path[1]][$path[2]] : $default;

case 4;
return isset($array[$path[0]][$path[1]][$path[2]][$path[3]]) ? $array[$path[0]][$path[1]][$path[2]][$path[3]] : $default;

case 5;
return isset($array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]]) ? $array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]] : $default;

case 6;
return isset($array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]][$path[5]]) ? $array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]][$path[5]] : $default;

case 7;
return isset($array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]][$path[5]][$path[6]])
? $array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]][$path[5]][$path[6]] : $default;

case 8;
return isset($array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]][$path[5]][$path[6]][$path[7]])
? $array[$path[0]][$path[1]][$path[2]][$path[3]][$path[4]][$path[5]][$path[6]][$path[7]] : $default;

default:
throw new CRM_Core_Exception('More levels in array than are supported');
}
}

}

0 comments on commit 70cf3be

Please sign in to comment.