<customData>
<customValue>
<id></id>
<value></value>
</customValue>
<!-- ... -->
</customData>
'customData' => array(
'customValue' => array(
array(
'id' => 13850,
'value' => $visit
)
),
'customValue' => array(
array(
'id' => 13852,
'value' => $id
)
),
'customValue' => array(
array(
'id' => 13854,
'value' => $ga
)
)
)
protected function importParams($requestXml, $params) {
foreach($params as $key => $val) {
if (is_array($val)) {
$requestXml->$key = new SimpleXMLElement("<$key/>");
foreach($val as $key2 => $val2) {
if (is_array($val2)) {
$this->importParams($requestXml->$key, $val2);
} else {
$requestXml->$key->addChild($key2, $val2);
}
}
} else {
$requestXml->addChild($key, $val);
}
}
return $requestXml;
}
'customData' => array(
array(
'customValue' => array(
array(
'id' => 13850,
'value' => $visit
)
),
),
array(
'customValue' => array(
array(
'id' => 13852,
'value' => $id
)
),
),
array(
'customValue' => array(
array(
'id' => 13854,
'value' => $ga
)
)
)
)
class XmlConverter
{
/**
* Conver array in xml
*/
public function as_array(array $data, $xml = NULL)
{
if (is_null($xml))
{
$xml = simplexml_load_string('<'.key($data).'/>');
$data = current($data);
$return = TRUE;
}
if (is_array($data))
{
foreach ($data as $name => $value)
{
self::from_array($value, is_numeric($name) ? $xml : $xml->addChild($name));
}
}
else
{
$xml->{0} = $data;
}
if ( ! empty($return))
{
return $xml->asXML();
}
}
/**
* Conver xml in array
*/
public function to_array($xml)
{
$tree = NULL;
while($xml->read())
{
switch ($xml->nodeType)
{
case XMLReader::END_ELEMENT:
return $tree;
case XMLReader::ELEMENT:
$node = array(
'tag' => $xml->name,
'value' => $xml->isEmptyElement ? '' : self::to_array($xml)
);
if ($xml->hasAttributes)
{
while ($xml->moveToNextAttribute())
{
$node['attributes'][$xml->name] = $xml->value;
}
}
$tree[] = $node;
break;
case XMLReader::TEXT:
case XMLReader::CDATA:
$tree .= $xml->value;
}
}
return $tree;
}
}
$requestXml->$key = new SimpleXMLElement("<$key/>");
$requestXml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");