Merge pull request #11907 from mattwire/contribution_financialtype_fix
[civicrm-core.git] / CRM / Utils / Array.php
index 4829078fc043dabbe7eceacc001bc6c5abf894e4..a9d6cdd07ef50c53c1d8393adf9aa1a772b23222 100644 (file)
@@ -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;
+  }
+
 }