BMLT Root Server
Array2XML.php
Go to the documentation of this file.
1 <?php
2 /**
3 * \brief array2xml() will convert any given array into a XML structure.
4 *
5 * \version: 1.0
6 *
7 * \author: Marcus Carver © 2008
8 *
9 * Email: marcuscarver@gmail.com
10 *
11 * Link: http://marcuscarver.blogspot.com/
12 *
13 * Arguments : $array - The array you wish to convert into a XML structure.
14 * $name - The name you wish to enclose the array in, the 'parent' tag for XML.
15 * $beginning - INTERNAL USE... DO NOT USE!
16 *
17 * \return: Gives a string output in a XML structure
18 *
19 * Use: echo array2xml($products,'products');
20 * die;
21 
22  This file is part of the Basic Meeting List Toolbox (BMLT).
23 
24  Find out more at: https://bmlt.app
25 
26  BMLT is free software: you can redistribute it and/or modify
27  it under the terms of the MIT License.
28 
29  BMLT is distributed in the hope that it will be useful,
30  but WITHOUT ANY WARRANTY; without even the implied warranty of
31  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32  MIT License for more details.
33 
34  You should have received a copy of the MIT License along with this code.
35  If not, see <https://opensource.org/licenses/MIT>.
36 */
37 
38 function array2xml(
39  $array, ///< The input array
40  $name = 'array', ///< The name that you want as the root element for the XML output
41  $beginning = true ///< Used for the recursive parser. Not for external use.
42 ) {
43  global $nested, $s_array2xml_index;
44 
45  $output = '';
46 
47  if ($beginning) {
48  $output = '<' . htmlspecialchars($name) . '>';
49  $s_array2xml_index = 0;
50  $nested = 0;
51  }
52 
53  // This is required because XML standards do not allow a tag to start with a number or symbol, you can change this value to whatever you like:
54  $ArrayNumberPrefix = 'row';
55 
56  foreach ($array as $root => $child) {
57  if (is_array($child)) {
58  $output .= '<' . (is_string($root) ? htmlspecialchars($root) : $ArrayNumberPrefix) . ' sequence_index="'.strval(intval($s_array2xml_index++)).'">';
59  $nested++;
60  $output .= array2xml($child, null, null, false);
61  $nested--;
62  $output .= '</' . (is_string($root) ? htmlspecialchars($root) : $ArrayNumberPrefix) . '>';
63  } elseif (isset($child) && $child) {
64  $output .= '<' . (is_string($root) ? htmlspecialchars($root) : $ArrayNumberPrefix . htmlspecialchars($root)) . '>' . htmlspecialchars($child) . '</' . (is_string($root) ? htmlspecialchars($root) : $ArrayNumberPrefix) . '>';
65  } else {
66 // Commented out, because we will simply not add empty elements (for now). This is to save bandwidth.
67 // $output .= '<' . (is_string($root) ? htmlspecialchars($root) : $ArrayNumberPrefix . htmlspecialchars($root)) . '/>';
68  }
69  }
70 
71  if ($beginning) {
72  $output .= '</' . htmlspecialchars($name) . '>';
73  }
74 
75  return $output;
76 }
array2xml($array, $name= 'array', $beginning=true)
array2xml() will convert any given array into a XML structure.
Definition: Array2XML.php:38