Merge pull request #7886 from sarehag/CRM-18114
[civicrm-core.git] / CRM / Utils / Array.php
index 57c03e139101c7eab2726a0024b73c45fddf1e80..17fc32963be70b71f824b90dcf4b04018c39374a 100644 (file)
@@ -106,7 +106,7 @@ class CRM_Utils_Array {
    * @return int|string|null
    *   Returns the key, which could be an int or a string, or NULL on failure.
    */
-  public static function key($value, &$list) {
+  public static function key($value, $list) {
     if (is_array($list)) {
       $key = array_search($value, $list);
 
@@ -132,7 +132,6 @@ class CRM_Utils_Array {
    * @param string $seperator
    *   (optional) String to be appended after open/close tags.
    *
-   *
    * @return string
    *   XML fragment representing $list.
    */
@@ -345,8 +344,11 @@ class CRM_Utils_Array {
   }
 
   /**
-   * @param $subset
-   * @param $superset
+   * Is array A a subset of array B.
+   *
+   * @param array $subset
+   * @param array $superset
+   *
    * @return bool
    *   TRUE if $subset is a subset of $superset
    */
@@ -388,12 +390,18 @@ class CRM_Utils_Array {
   }
 
   /**
-   * convert associative array names to values.
-   * and vice-versa.
+   * Convert associative array names to values and vice-versa.
    *
    * This function is used by both the web form layer and the api. Note that
    * the api needs the name => value conversion, also the view layer typically
    * requires value => name conversion
+   *
+   * @param array $defaults
+   * @param string $property
+   * @param $lookup
+   * @param $reverse
+   *
+   * @return bool
    */
   public static function lookupValue(&$defaults, $property, $lookup, $reverse) {
     $id = $property . '_id';
@@ -1018,4 +1026,25 @@ class CRM_Utils_Array {
     return $result;
   }
 
+  /**
+   * Convert array where key(s) holds the actual value and value(s) as 1 into array of actual values
+   *  Ex: array('foobar' => 1, 4 => 1) formatted into array('foobar', 4)
+   *
+   * @param array $array
+   * @return void
+   */
+  public static function formatArrayKeys(&$array) {
+    $keys = array_keys($array, 1);
+    if (count($keys) > 1 ||
+      (count($keys) == 1 &&
+        (current($keys) > 1 ||
+          is_string(current($keys)) ||
+          (current($keys) == 1 && $array[1] == 1) // handle (0 => 4), (1 => 1)
+        )
+      )
+    ) {
+      $array = $keys;
+    }
+  }
+
 }