Chris@0: 'text_format', Chris@0: * '#title' => t('Signature'), Chris@0: * ); Chris@0: * // Or, it might be further nested: Chris@0: * $form['signature_settings']['user']['signature'] = array( Chris@0: * '#type' => 'text_format', Chris@0: * '#title' => t('Signature'), Chris@0: * ); Chris@0: * @endcode Chris@0: * Chris@0: * To deal with the situation, the code needs to figure out the route to the Chris@0: * element, given an array of parents that is either Chris@0: * @code array('signature_settings', 'signature') @endcode Chris@0: * in the first case or Chris@0: * @code array('signature_settings', 'user', 'signature') @endcode Chris@0: * in the second case. Chris@0: * Chris@0: * Without this helper function the only way to set the signature element in Chris@0: * one line would be using eval(), which should be avoided: Chris@0: * @code Chris@0: * // Do not do this! Avoid eval(). Chris@0: * eval('$form[\'' . implode("']['", $parents) . '\'] = $element;'); Chris@0: * @endcode Chris@0: * Chris@0: * Instead, use this helper function: Chris@0: * @code Chris@0: * NestedArray::setValue($form, $parents, $element); Chris@0: * @endcode Chris@0: * Chris@0: * However if the number of array parent keys is static, the value should Chris@0: * always be set directly rather than calling this function. For instance, Chris@0: * for the first example we could just do: Chris@0: * @code Chris@0: * $form['signature_settings']['signature'] = $element; Chris@0: * @endcode Chris@0: * Chris@0: * @param array $array Chris@0: * A reference to the array to modify. Chris@0: * @param array $parents Chris@0: * An array of parent keys, starting with the outermost key. Chris@0: * @param mixed $value Chris@0: * The value to set. Chris@0: * @param bool $force Chris@0: * (optional) If TRUE, the value is forced into the structure even if it Chris@0: * requires the deletion of an already existing non-array parent value. If Chris@0: * FALSE, PHP throws an error if trying to add into a value that is not an Chris@0: * array. Defaults to FALSE. Chris@0: * Chris@0: * @see NestedArray::unsetValue() Chris@0: * @see NestedArray::getValue() Chris@0: */ Chris@0: public static function setValue(array &$array, array $parents, $value, $force = FALSE) { Chris@0: $ref = &$array; Chris@0: foreach ($parents as $parent) { Chris@0: // PHP auto-creates container arrays and NULL entries without error if $ref Chris@0: // is NULL, but throws an error if $ref is set, but not an array. Chris@0: if ($force && isset($ref) && !is_array($ref)) { Chris@0: $ref = []; Chris@0: } Chris@0: $ref = &$ref[$parent]; Chris@0: } Chris@0: $ref = $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Unsets a value in a nested array with variable depth. Chris@0: * Chris@0: * This helper function should be used when the depth of the array element you Chris@0: * are changing may vary (that is, the number of parent keys is variable). It Chris@0: * is primarily used for form structures and renderable arrays. Chris@0: * Chris@0: * Example: Chris@0: * @code Chris@0: * // Assume you have a 'signature' element somewhere in a form. It might be: Chris@0: * $form['signature_settings']['signature'] = array( Chris@0: * '#type' => 'text_format', Chris@0: * '#title' => t('Signature'), Chris@0: * ); Chris@0: * // Or, it might be further nested: Chris@0: * $form['signature_settings']['user']['signature'] = array( Chris@0: * '#type' => 'text_format', Chris@0: * '#title' => t('Signature'), Chris@0: * ); Chris@0: * @endcode Chris@0: * Chris@0: * To deal with the situation, the code needs to figure out the route to the Chris@0: * element, given an array of parents that is either Chris@0: * @code array('signature_settings', 'signature') @endcode Chris@0: * in the first case or Chris@0: * @code array('signature_settings', 'user', 'signature') @endcode Chris@0: * in the second case. Chris@0: * Chris@0: * Without this helper function the only way to unset the signature element in Chris@0: * one line would be using eval(), which should be avoided: Chris@0: * @code Chris@0: * // Do not do this! Avoid eval(). Chris@0: * eval('unset($form[\'' . implode("']['", $parents) . '\']);'); Chris@0: * @endcode Chris@0: * Chris@0: * Instead, use this helper function: Chris@0: * @code Chris@0: * NestedArray::unset_nested_value($form, $parents, $element); Chris@0: * @endcode Chris@0: * Chris@0: * However if the number of array parent keys is static, the value should Chris@0: * always be set directly rather than calling this function. For instance, for Chris@0: * the first example we could just do: Chris@0: * @code Chris@0: * unset($form['signature_settings']['signature']); Chris@0: * @endcode Chris@0: * Chris@0: * @param array $array Chris@0: * A reference to the array to modify. Chris@0: * @param array $parents Chris@0: * An array of parent keys, starting with the outermost key and including Chris@0: * the key to be unset. Chris@0: * @param bool $key_existed Chris@0: * (optional) If given, an already defined variable that is altered by Chris@0: * reference. Chris@0: * Chris@0: * @see NestedArray::setValue() Chris@0: * @see NestedArray::getValue() Chris@0: */ Chris@0: public static function unsetValue(array &$array, array $parents, &$key_existed = NULL) { Chris@0: $unset_key = array_pop($parents); Chris@0: $ref = &self::getValue($array, $parents, $key_existed); Chris@14: if ($key_existed && is_array($ref) && (isset($ref[$unset_key]) || array_key_exists($unset_key, $ref))) { Chris@0: $key_existed = TRUE; Chris@0: unset($ref[$unset_key]); Chris@0: } Chris@0: else { Chris@0: $key_existed = FALSE; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines whether a nested array contains the requested keys. Chris@0: * Chris@0: * This helper function should be used when the depth of the array element to Chris@0: * be checked may vary (that is, the number of parent keys is variable). See Chris@0: * NestedArray::setValue() for details. It is primarily used for form Chris@0: * structures and renderable arrays. Chris@0: * Chris@0: * If it is required to also get the value of the checked nested key, use Chris@0: * NestedArray::getValue() instead. Chris@0: * Chris@0: * If the number of array parent keys is static, this helper function is Chris@0: * unnecessary and the following code can be used instead: Chris@0: * @code Chris@0: * $value_exists = isset($form['signature_settings']['signature']); Chris@0: * $key_exists = array_key_exists('signature', $form['signature_settings']); Chris@0: * @endcode Chris@0: * Chris@0: * @param array $array Chris@0: * The array with the value to check for. Chris@0: * @param array $parents Chris@0: * An array of parent keys of the value, starting with the outermost key. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if all the parent keys exist, FALSE otherwise. Chris@0: * Chris@0: * @see NestedArray::getValue() Chris@0: */ Chris@0: public static function keyExists(array $array, array $parents) { Chris@0: // Although this function is similar to PHP's array_key_exists(), its Chris@0: // arguments should be consistent with getValue(). Chris@0: $key_exists = NULL; Chris@0: self::getValue($array, $parents, $key_exists); Chris@0: return $key_exists; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Merges multiple arrays, recursively, and returns the merged array. Chris@0: * Chris@0: * This function is similar to PHP's array_merge_recursive() function, but it Chris@0: * handles non-array values differently. When merging values that are not both Chris@0: * arrays, the latter value replaces the former rather than merging with it. Chris@0: * Chris@0: * Example: Chris@0: * @code Chris@0: * $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => t('X'), 'class' => array('a', 'b'))); Chris@0: * $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('c', 'd'))); Chris@0: * Chris@0: * // This results in array('fragment' => array('x', 'y'), 'attributes' => array('title' => array(t('X'), t('Y')), 'class' => array('a', 'b', 'c', 'd'))). Chris@0: * $incorrect = array_merge_recursive($link_options_1, $link_options_2); Chris@0: * Chris@0: * // This results in array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('a', 'b', 'c', 'd'))). Chris@0: * $correct = NestedArray::mergeDeep($link_options_1, $link_options_2); Chris@0: * @endcode Chris@0: * Chris@0: * @param array ... Chris@0: * Arrays to merge. Chris@0: * Chris@0: * @return array Chris@0: * The merged array. Chris@0: * Chris@0: * @see NestedArray::mergeDeepArray() Chris@0: */ Chris@0: public static function mergeDeep() { Chris@0: return self::mergeDeepArray(func_get_args()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Merges multiple arrays, recursively, and returns the merged array. Chris@0: * Chris@0: * This function is equivalent to NestedArray::mergeDeep(), except the Chris@0: * input arrays are passed as a single array parameter rather than a variable Chris@0: * parameter list. Chris@0: * Chris@0: * The following are equivalent: Chris@0: * - NestedArray::mergeDeep($a, $b); Chris@0: * - NestedArray::mergeDeepArray(array($a, $b)); Chris@0: * Chris@0: * The following are also equivalent: Chris@0: * - call_user_func_array('NestedArray::mergeDeep', $arrays_to_merge); Chris@0: * - NestedArray::mergeDeepArray($arrays_to_merge); Chris@0: * Chris@0: * @param array $arrays Chris@0: * An arrays of arrays to merge. Chris@0: * @param bool $preserve_integer_keys Chris@0: * (optional) If given, integer keys will be preserved and merged instead of Chris@0: * appended. Defaults to FALSE. Chris@0: * Chris@0: * @return array Chris@0: * The merged array. Chris@0: * Chris@0: * @see NestedArray::mergeDeep() Chris@0: */ Chris@0: public static function mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE) { Chris@0: $result = []; Chris@0: foreach ($arrays as $array) { Chris@0: foreach ($array as $key => $value) { Chris@0: // Renumber integer keys as array_merge_recursive() does unless Chris@0: // $preserve_integer_keys is set to TRUE. Note that PHP automatically Chris@0: // converts array keys that are integer strings (e.g., '1') to integers. Chris@0: if (is_int($key) && !$preserve_integer_keys) { Chris@0: $result[] = $value; Chris@0: } Chris@0: // Recurse when both values are arrays. Chris@0: elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) { Chris@0: $result[$key] = self::mergeDeepArray([$result[$key], $value], $preserve_integer_keys); Chris@0: } Chris@0: // Otherwise, use the latter value, overriding any previous value. Chris@0: else { Chris@0: $result[$key] = $value; Chris@0: } Chris@0: } Chris@0: } Chris@0: return $result; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Filters a nested array recursively. Chris@0: * Chris@0: * @param array $array Chris@0: * The filtered nested array. Chris@0: * @param callable|null $callable Chris@0: * The callable to apply for filtering. Chris@0: * Chris@0: * @return array Chris@0: * The filtered array. Chris@0: */ Chris@0: public static function filter(array $array, callable $callable = NULL) { Chris@0: $array = is_callable($callable) ? array_filter($array, $callable) : array_filter($array); Chris@0: foreach ($array as &$element) { Chris@0: if (is_array($element)) { Chris@0: $element = static::filter($element, $callable); Chris@0: } Chris@0: } Chris@0: Chris@0: return $array; Chris@0: } Chris@0: Chris@0: }