function actionArray() { echo 'Getting a XML from an ARRAY<br /><br />'; $xml = new YDXml(); $xml->loadString($this->contents); $array = $xml->toArray(); YDDebugUtil::dump(YDXml::traverse($array), 'YDXml::traverse of the ARRAY - static call'); $xml = new YDXml(); // not really needed - loadArray always resets the object before loading an array $xml->loadArray($array); YDDebugUtil::dump($xml->traverse(), 'YDXml::traverse after loading the ARRAY'); }
/** * This function returns the XML as a string representation of the array * returned by toArray. * * @param $array (Optional) The array to traverse. If null, the result from * toArray of the current object. Default: null. * @param $name (Optional) The name of the array. Default: array. * @param $level (Optional) The level of parsing the array. * * @returns A string representation of the array returned by toArray. * * @static If $array is passed. */ function traverse($array = null, $name = 'array', $level = 0) { if (is_null($array)) { $array = $this->toArray(); } $traverse = array(); if (is_array($array)) { foreach ($array as $key => $val) { if (is_array($val)) { $traverse = array_merge($traverse, YDXml::traverse($val, $name . "[" . $key . "]", $level + 1)); } else { $traverse[] = '$' . $name . '[' . $key . '] = "' . str_replace("\n", "", trim($val)) . "\"\n"; } } } return implode('', $traverse); }