arrays
nested_array_merge_deep.php
use Drupal\Component\Utility\NestedArray;
// Parent array with default settings.
$parent_array = [
'config' => [
'theme' => 'dark',
'layout' => [
'width' => 800,
'height' => 600,
],
],
];
// Child array adds more nested data but doesn't override existing values.
$child_array = [
'config' => [
'layout' => [
'depth' => 300, // New nested key
],
'features' => [
'animations' => true, // New section
],
],
];
// Merge both arrays.
$merged_array = NestedArray::mergeDeep($parent_array, $child_array);nested_array_result.php
[
'config' => [
// Unchanged (not overridden)
'theme' => 'dark',
'layout' => [
// Keeps parent value
'width' => 800,
// Keeps parent value
'height' => 600,
// New nested key added
'depth' => 300,
],
'features' => [
// New section added
'animations' => true,
],
],
]
30.07.2026 | Lothar Ferreira Neumann
Merge multidimensional arrays in Drupal
downcastArray.php
$array = [
'key_one' => 'one',
'key_two' => 'two',
'key_three' => 'three',
];
// Downcast associated array to indicated array with only keys.
array_keys($array);
// Downcast associated array to indicated array with only values.
array_values($array);ExampleOutcome.txt
Outcome array_keys():
$array = [
[0] = 'key_one',
[1] = 'key_two',
[2] = 'key_three',
];
Outcome array_values():
$array = [
[0] = 'one',
[1] = 'two',
[2] = 'three',
];
04.04.2024 | Lothar Ferreira Neumann
Downcast an array.
UserDashboardController.php
$menu_name = 'my_custom_menu_machine_name';
$menu_tree = \Drupal::menuTree();
// Build the typical default set of menu tree parameters.
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
// Load the tree based on this set of parameters.
$tree = $menu_tree->load($menu_name, $parameters);
// Transform the tree using the manipulators you want.
$manipulators = [
// Only show links that are accessible for the current user.
['callable' => 'menu.default_tree_manipulators:checkAccess'],
// Use the default sorting of menu links.
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
];
$tree = $menu_tree->transform($tree, $manipulators);
$menu = $menu_tree->build($tree);
// Set theme for custom styling.
$menu['#theme'] = 'my_custom_theme';
12.03.2026 | Peter Gerken