You are here

function _entity_bundle_plugin_array_merge_deep_array in Entity bundle plugin 7

Merges multiple arrays, recursively, and returns the merged array.

This function is pretty much a clone of drupal_array_merge_deep_array() and similar to PHP's array_merge_recursive() function, but it handles non-array values differently. When merging values that are not both arrays, the latter value replaces the former rather than merging with it, and numeric keys are also merged, NOT appended.

Example:

$array_1 = array(
  'test' => array(
    'X',
  ),
  array(
    0 => 'A',
    1 => 'B',
  ),
);
$array_2 = array(
  'test' => array(
    'Y',
  ),
  array(
    0 => 'C',
    1 => 'D',
  ),
);

// This results in array('test' => array('X', 'Y'), 0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D').
$incorrect = drupal_array_merge_deep_array(array(
  $array_1,
  $array_2,
));

// This results in array('test' => array('X', 'Y'), 0 => 'C', 1 => 'D').
$correct = _entity_bundle_plugin_array_merge_deep_array(array(
  $array_1,
  $array_2,
));

Parameters

array $arrays: Arrays to merge.

Return value

array The merged array.

See also

drupal_array_merge_deep_array()

2 calls to _entity_bundle_plugin_array_merge_deep_array()
entity_bundle_plugin_merge::testMerge in tests/entity_bundle_plugin_merge.test
entity_bundle_plugin_rebuild_fields in ./entity_bundle_plugin.module
Rebuild the fields for a given entity type.

File

./entity_bundle_plugin.module, line 202
EntityBundlePlugin module.

Code

function _entity_bundle_plugin_array_merge_deep_array(array $arrays) {
  $result = array();
  foreach ($arrays as $array) {
    foreach ($array as $key => $value) {

      // Recurse when both values are arrays.
      if (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
        $result[$key] = _entity_bundle_plugin_array_merge_deep_array(array(
          $result[$key],
          $value,
        ));
      }
      else {
        $result[$key] = $value;
      }
    }
  }
  return $result;
}