[ * 'child' => [ * ['name' => 'Wendy Darling'], * ['name' => 'John Darling'], * ['name' => 'Michael Darling'], * ], * ]] * * Output XML = * * Wendy Darling * John Darling * Michael Darling * * * @static * @param array $array array to convert into XML * @return string generated XML */ public static function arrayToXML($array, $format = false, $level = 0) { $xml = ''; if (is_array($array)) { foreach ($array as $node => $value) { // Checks if the value is an array if (is_array($value)) { foreach ($value as $node2 => $value2) { if (is_array($value2)) { // Nest the value if the node is an integer $new_value = (is_int($node2) ? $value2 : [$node2 => $value2]); $xml .= ($format ? str_repeat("\t", $level) : ''); $xml .= '<' . $node . '>' . ($format ? "\n" : ''); $xml .= self::arrayToXML($new_value, $format, $level + 1); $xml .= ($format ? str_repeat("\t", $level) : ''); $xml .= '' . ($format ? "\n" : ''); } else { if (is_int($node2)) { $node2 = $node; } // Checks for special characters if (htmlspecialchars($value2) != $value2) { $xml .= ($format ? str_repeat("\t", $level) : ''); $xml .= '<' . $node2 . '>' . ($format ? "\n" : ''); } else { $xml .= ($format ? str_repeat("\t", $level) : ''); $xml .= '<' . $node2 . '>' . $value2 . '' . ($format ? "\n" : ''); } } } } else { // Checks for special characters if (htmlspecialchars($value) != $value) { $xml .= ($format ? str_repeat("\t", $level) : ''); $xml .= '<' . $node . '>' . ($format ? "\n" : ''); } else { $xml .= ($format ? str_repeat("\t", $level) : ''); $xml .= '<' . $node . '>' . $value . '' . ($format ? "\n" : ''); } } } } return $xml; } // }}} }