BMLT Root Server
Functions
Array2XML.php File Reference

Go to the source code of this file.

Functions

 array2xml ($array, $name= 'array', $beginning=true)
 array2xml() will convert any given array into a XML structure. More...
 

Function Documentation

array2xml (   $array,
  $name = 'array',
  $beginning = true 
)

array2xml() will convert any given array into a XML structure.

Version
: 1.0
Author
: Marcus Carver © 2008

Email: marcu.nosp@m.scar.nosp@m.ver@g.nosp@m.mail.nosp@m..com

Link: http://marcuscarver.blogspot.com/

Arguments : $array - The array you wish to convert into a XML structure. $name - The name you wish to enclose the array in, the 'parent' tag for XML. $beginning - INTERNAL USE... DO NOT USE!

Returns
: Gives a string output in a XML structure

Use: echo array2xml($products,'products'); die;

This file is part of the Basic Meeting List Toolbox (BMLT).

Find out more at: https://bmlt.app

BMLT is free software: you can redistribute it and/or modify it under the terms of the MIT License.

BMLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MIT License for more details.

You should have received a copy of the MIT License along with this code. If not, see https://opensource.org/licenses/MIT.

Parameters
$arrayThe input array
$nameThe name that you want as the root element for the XML output
$beginningUsed for the recursive parser. Not for external use.

Definition at line 38 of file Array2XML.php.

Referenced by c_comdef_admin_xml_handler\TranslateCSVToXML(), and TranslateToXML().

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