Merge pull request #21303 from civicrm/5.41
[civicrm-core.git] / CRM / Utils / Array.php
index 99cd769b5dddb8d72447d23ee4bdee1e6a6e75d5..bbc931c38f91e27c54393b4f359c6dfdd1b164f3 100644 (file)
@@ -1142,6 +1142,31 @@ class CRM_Utils_Array {
     $r[$last] = $value;
   }
 
+  /**
+   * Move an item in an array-tree (if it exists).
+   *
+   * @param array $values
+   *   Data-tree
+   * @param string[] $src
+   *   Old path for the existing item
+   * @param string[] $dest
+   *   New path
+   * @param bool $cleanup
+   * @return int
+   *   Number of items moved (0 or 1).
+   */
+  public static function pathMove(&$values, $src, $dest, $cleanup = FALSE) {
+    if (!static::pathIsset($values, $src)) {
+      return 0;
+    }
+    else {
+      $value = static::pathGet($values, $src);
+      static::pathSet($values, $dest, $value);
+      static::pathUnset($values, $src, $cleanup);
+      return 1;
+    }
+  }
+
   /**
    * Convert a simple dictionary into separate key+value records.
    *