public static function NestedArray::setValue in Blazy 7
Sets a value in a nested array with variable depth.
Parameters
array $array: A reference to the array to modify.
array $parents: An array of parent keys, starting with the outermost key.
mixed $value: The value to set.
bool $force: (optional) If TRUE, the value is forced into the structure even if it requires the deletion of an already existing non-array parent value. If FALSE, PHP throws an error if trying to add into a value that is not an array. Defaults to FALSE.
See also
File
- src/
Utility/ NestedArray.php, line 67
Class
- NestedArray
- Provides helpers to perform operations on nested arrays, kidnapped from D8.
Namespace
Drupal\blazy\UtilityCode
public static function setValue(array &$array, array $parents, $value, $force = FALSE) {
$ref =& $array;
foreach ($parents as $parent) {
// PHP auto-creates container arrays and NULL entries without error if
// $ref is NULL, but throws an error if $ref is set, but not an array.
if ($force && isset($ref) && !is_array($ref)) {
$ref = [];
}
$ref =& $ref[$parent];
}
$ref = $value;
}