X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FArray.php;h=a9d6cdd07ef50c53c1d8393adf9aa1a772b23222;hb=e0f29b28c437b9e490a81091360bf21ea24f46f2;hp=4829078fc043dabbe7eceacc001bc6c5abf894e4;hpb=f3ebf7b57a1c114d397b89cd4c0a76916351cfc5;p=civicrm-core.git diff --git a/CRM/Utils/Array.php b/CRM/Utils/Array.php index 4829078fc0..a9d6cdd07e 100644 --- a/CRM/Utils/Array.php +++ b/CRM/Utils/Array.php @@ -1197,4 +1197,32 @@ class CRM_Utils_Array { return $array; } + /** + * Append the value to the array using the key provided. + * + * e.g if value is 'llama' & path is [0, 'email', 'location'] result will be + * [0 => ['email' => ['location' => 'llama']] + * + * @param $path + * @param $value + * @param array $source + * + * @return array + */ + public static function recursiveBuild($path, $value, $source = []) { + $arrayKey = array_shift($path); + // Recurse through array keys + if ($path) { + if (!isset($source[$arrayKey])) { + $source[$arrayKey] = []; + } + $source[$arrayKey] = self::recursiveBuild($path, $value, $source[$arrayKey]); + } + // Final iteration + else { + $source[$arrayKey] = $value; + } + return $source; + } + }