You are here

function twig_array_merge in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/twig/twig/lib/Twig/Extension/Core.php \twig_array_merge()

Merges an array with another one.

<pre> {% set items = { 'apple': 'fruit', 'orange': 'fruit' } %}

{% set items = items|merge({ 'peugeot': 'car' }) %}

{# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #} </pre>

Parameters

array|Traversable $arr1 An array:

array|Traversable $arr2 An array:

Return value

array The merged array

1 string reference to 'twig_array_merge'
Twig_Extension_Core::getFilters in vendor/twig/twig/lib/Twig/Extension/Core.php

File

vendor/twig/twig/lib/Twig/Extension/Core.php, line 687

Code

function twig_array_merge($arr1, $arr2) {
  if ($arr1 instanceof Traversable) {
    $arr1 = iterator_to_array($arr1);
  }
  elseif (!is_array($arr1)) {
    throw new Twig_Error_Runtime(sprintf('The merge filter only works with arrays or "Traversable", got "%s" as first argument.', gettype($arr1)));
  }
  if ($arr2 instanceof Traversable) {
    $arr2 = iterator_to_array($arr2);
  }
  elseif (!is_array($arr2)) {
    throw new Twig_Error_Runtime(sprintf('The merge filter only works with arrays or "Traversable", got "%s" as second argument.', gettype($arr2)));
  }
  return array_merge($arr1, $arr2);
}