You are here

public static function Utility::deepCopy in Search API 8

Returns a deep copy of the input array.

The behavior of PHP regarding arrays with references pointing to it is rather weird. Therefore, this method should be used when making a copy of such an array, or of an array containing references.

This method will also omit empty array elements (that is, elements that evaluate to FALSE according to PHP's native rules).

Parameters

array $array: The array to copy.

Return value

array A deep copy of the array.

2 calls to Utility::deepCopy()
template_preprocess_search_api_index in ./search_api.theme.inc
Prepares variables for search_api_index templates.
template_preprocess_search_api_server in ./search_api.theme.inc
Prepares variables for search_api_server form templates.

File

src/Utility/Utility.php, line 51

Class

Utility
Contains utility methods for the Search API.

Namespace

Drupal\search_api\Utility

Code

public static function deepCopy(array $array) {
  $copy = [];
  foreach ($array as $k => $v) {
    if (is_array($v)) {
      if ($v = static::deepCopy($v)) {
        $copy[$k] = $v;
      }
    }
    elseif (is_object($v)) {
      $copy[$k] = clone $v;
    }
    elseif ($v) {
      $copy[$k] = $v;
    }
  }
  return $copy;
}