You are here

protected function CsvEncoder::arrayDepth in CSV Serialization 8

Same name and namespace in other branches
  1. 8.2 src/Encoder/CsvEncoder.php \Drupal\csv_serialization\Encoder\CsvEncoder::arrayDepth()

Determine the depth of an array.

This method determines array depth by analyzing the indentation of the dumped array. This avoid potential issues with recursion.

Parameters

array $array: The array to measure.

Return value

float The depth of the array.

See also

http://stackoverflow.com/a/263621

1 call to CsvEncoder::arrayDepth()
CsvEncoder::flattenCell in src/Encoder/CsvEncoder.php
Flattens a multi-dimensional array into a single level.

File

src/Encoder/CsvEncoder.php, line 336

Class

CsvEncoder
Adds CSV encoder support for the Serialization API.

Namespace

Drupal\csv_serialization\Encoder

Code

protected function arrayDepth(array $array) {
  $max_indentation = 1;
  $array_str = print_r($array, TRUE);
  $lines = explode("\n", $array_str);
  foreach ($lines as $line) {
    $indentation = (strlen($line) - strlen(ltrim($line))) / 4;
    if ($indentation > $max_indentation) {
      $max_indentation = $indentation;
    }
  }
  return ceil(($max_indentation - 1) / 2) + 1;
}