0

I have this array

$arr = ['field_event_start_date', 'widget', 0, 'value', '#date_part_order', 3]

And I want to convert this into this array in below format

$form['field_event_start_date']['widget'][0]['value']['#date_part_order'][3]

Trying to append the array to $form array.

I have a $form array, which contains many fields, and I want to unset a set of fields. The array above, is part of a bigger array. If I take this array, How do I convert this into array like below

$form['field_event_start_date']['widget'][0]['value']['#date_part_order'][3]

I have tried, implode like this $form[implode('][', $arr)] But this always says, undefined index.

Final code I need to run is

unset($form['field_event_start_date']['widget'][0]['value']['#date_part_order'][3]);

However, there are many like this, which number of depth varying. I need to write a generic function to achieve this.

2
  • instead of unsetting existing array, replace its value. Commented Dec 26, 2016 at 5:08
  • For that as well, I need to reach that point which contains the value. The question is, how do i build the above statement from a given array. Commented Dec 26, 2016 at 5:33

2 Answers 2

1

In Drupal 8, you can use NestedArray::unsetValue() to unset a value is nested arrays. You can also use NestedArray::getValue() and NestedArray::setValue() to get and set value from nested arrays using a array of keys ($parents).

Drupal 7 only have drupal_array_get_nested_value() and drupal_array_set_nested_value(). But no drupal_array_unset_nested_value(). It can easily be ported from Drupal 8:

/**
 * Unsets a value in a nested array with variable depth.
 *
 * This helper function should be used when the depth of the array element you
 * are changing may vary (that is, the number of parent keys is variable). It
 * is primarily used for form structures and renderable arrays.
 *
 * @param array $array
 *   A reference to the array to modify.
 * @param array $parents
 *   An array of parent keys, starting with the outermost key and including
 *   the key to be unset.
 * @param bool $key_existed
 *   (optional) If given, an already defined variable that is altered by
 *   reference.
 *
 * Port of NestedArray::unsetValue() from Drupal 8
 *
 * @see drupal_array_get_nested_value()
 * @see drupal_array_set_nested_value()
 */
function drupal_array_unset_nested_value(array &$array, array $parents, &$key_existed = NULL) {
  $unset_key = array_pop($parents);
  $ref = &drupal_array_get_nested_value($array, $parents, $key_existed);
  if ($key_existed && is_array($ref) && array_key_exists($unset_key, $ref)) {
    $key_existed = TRUE;
    unset($ref[$unset_key]);
  }
  else {
    $key_existed = FALSE;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

How about this:

eval("\$form['".implode("']['", $arr)."']='';");

1 Comment

That didn't work. I tried this on CLI. php eval("\$form['".implode("']['", ['a', 'b'])."']='';"); -bash: syntax error near unexpected token ('`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.