0

I Have an Array that I want a foreach to display the Key and Value as XML. The Page is coming up with <$value> and that is it? Any Ideas? Thank You!

$XML_a = array ("Ticket_ID" => "12343456");
    $query = "";
 $string_top = 
"<varcor_api>
    <response>";
       foreach ($XML_a as $key => $value) {
          $query .= "<" . $key . "><" . $value . "></" . $key . ">";
       }
$string_bottom = "
    </response>
</varcor_api>
";
    echo $string_top . $query . $string_bottom;
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
T Varcor
  • 375
  • 1
  • 2
  • 11
  • You did you try closing you your tags for the value like `"<$value />"`? Also i would really use `DOMDocument` for this.... – prodigitalson Jan 13 '15 at 21:18
  • Is this "<" . $key . "><" . $value . ">" . $key . ">" supposed to be "<" . $key . ">" . $value . "" . $key . ">" – Dave Goten Jan 13 '15 at 21:18
  • 1
    Why manually codify an XML document instead of using a [PHP library](http://stackoverflow.com/a/487629/59087) or a [database call](https://wiki.postgresql.org/wiki/XML_Support)? – Dave Jarvis Jan 13 '15 at 21:19
  • Dave Jarvis this is going to be an API Call, I am using MariaDB and PHP to get the Ticket_ID then generating a SimpleXMLElement to display the XML, Which it works now. Thank You Dave Goten I know better then to have <> for the Value of the XML Element. Appreciate It! – T Varcor Jan 13 '15 at 21:25

1 Answers1

0

It's bad idea to concatenate XML strings into XML structure. Instead, use Array to XML conversion algorithm - see Convert array to XML and How to convert array to SimpleXML.

Community
  • 1
  • 1
lubosdz
  • 4,210
  • 2
  • 29
  • 43