diff --git a/src/Encoder/CsvEncoder.php b/src/Encoder/CsvEncoder.php
index 5b5d396..30b3ec9 100644
--- a/src/Encoder/CsvEncoder.php
+++ b/src/Encoder/CsvEncoder.php
@@ -263,29 +263,33 @@ class CsvEncoder implements EncoderInterface, DecoderInterface {
   /**
    * 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.
-   *
    * @param $array
    * @return float
    *
-   * @see http://stackoverflow.com/a/263621
    */
   protected function arrayDepth($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;
+    $max_depth = 1;
+    $copy = $array;
+
+    foreach ($array as $key => $value) {
+      if (is_array($value)) {
+        // if its a reference stop here to prevent recursion.
+        $copy[$key]['__test'] == true;
+        if (isset($array[$key]['__test'])) {
+          unset($array[$key]['__test']);
+          $depth = 1;
+        }
+        else {
+          $depth = $this->arrayDepth($value) + 1;
+        }
+
+        if ($depth > $max_depth) {
+          $max_depth = $depth;
+        }
       }
     }
 
-    return ceil(($max_indentation - 1) / 2) + 1;
+    return $max_depth;
   }
 
   /**
