-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.php
96 lines (78 loc) · 2.11 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
function out( $msg, $obj ) {
if( DEBUG_MODE ) {
//if( !DEBUG_MODE ) echo '<!--';
echo '<p>'. $msg .'</p>';
echo '<pre>';
print_r( $obj );
echo '</pre>';
//if( !DEBUG_MODE ) echo '-->';
}
}
function parse_xml( $xml ) {
$xmlObj = simplexml_load_string($xml);
$arrXml = objectsIntoArray($xmlObj);
return $arrXml;
}
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
function dom_to_array($root)
{
$result = array();
if ($root->hasAttributes())
{
$attrs = $root->attributes;
foreach ($attrs as $i => $attr)
$result[$attr->name] = $attr->value;
}
$children = $root->childNodes;
if ($children->length == 1)
{
$child = $children->item(0);
if ($child->nodeType == XML_TEXT_NODE)
{
$result['_value'] = $child->nodeValue;
if (count($result) == 1)
return $result['_value'];
else
return $result;
}
}
$group = array();
for($i = 0; $i < $children->length; $i++)
{
$child = $children->item($i);
if (!isset($result[$child->nodeName]))
$result[$child->nodeName] = dom_to_array($child);
else
{
if (!isset($group[$child->nodeName]))
{
$tmp = $result[$child->nodeName];
$result[$child->nodeName] = array($tmp);
$group[$child->nodeName] = 1;
}
$result[$child->nodeName][] = dom_to_array($child);
}
}
return $result;
}
?>