depth = 1; $this->text = "\r\n<$root>\r\n"; $this->text .= $this->array_transform($array); $this->text .=""; return $this->text; } function array_transform($array) { $output = ''; $indent = str_repeat(' ', $this->depth * 4); $child_key = false; if (isset($array['__key'])) { $child_key = $array['__key']; unset($array['__key']); } foreach ($array as $key => $value) { if (!is_array($value)) { if (empty($key) || empty($value)) { continue; } $key = $child_key ? $child_key : $key; $output .= $indent . "<$key>" . htmlspecialchars($value, ENT_QUOTES) . "\r\n"; } else { $this->depth++; $key = $child_key ? $child_key : $key; $output_temp = $this->array_transform($value); if (!empty($output_temp)) { $output .= $indent . "<$key>\r\n"; $output .= $output_temp; $output .= $indent . "\r\n"; } $this->depth--; } } return $output; } } }