You are here

function CMBase::array2xml in Campaign Monitor 6.3

Same name and namespace in other branches
  1. 6.2 lib/CMBase.php \CMBase::array2xml()

* Converts an array to XML. This is the inverse to xml2array(). Values * are automatically escaped with htmlentities(), so you don't need to escape * values ahead of time. If you have, just set the third parameter to false. * This is an all-or-nothing deal. * *

Parameters

mixed $arr The associative to convert to an XML fragment: * @param string $indent (Optional) Starting identation of each element * @param string $escape (Optional) Determines whether or not to escape a text node. * @return string An XML fragment.

1 call to CMBase::array2xml()
CMBase::makeCall in lib/CMBase.php
* The direct way to make an API call. This allows developers to include new API * methods that might not yet have a wrapper method as part of the package. * *

File

lib/CMBase.php, line 492

Class

CMBase

Code

function array2xml($arr, $indent = '', $escape = true) {
  $buff = '';
  foreach ($arr as $k => $v) {

    // Dreamweaver has a bug which causes "/" to be stripped out of "</$k>"
    // when editing PHP files, so variable parsing is not longer being used
    // where a forward slash is required before the variable
    if (!is_array($v)) {
      $v = mb_detect_encoding($v, 'UTF-8', TRUE) == 'UTF-8' ? $v : utf8_encode($v);
      $buff .= "{$indent}<{$k}>" . ($escape ? htmlspecialchars($v) : $v) . "</" . $k . ">\n";
    }
    else {

      /*
      Encountered a list. The primary difference between the two branches is that
      in the 'if' branch, a $k element is generated for each item in $v, whereas
      in the 'else' branch, a single $k element encapsulates $v.
      */
      if (isset($v[0])) {
        foreach ($v as $_k => $_v) {
          if (is_array($_v)) {
            $buff .= "{$indent}<{$k}>\n" . $this
              ->array2xml($_v, $indent . "\t", $escape) . "{$indent}</" . $k . ">\n";
          }
          else {
            $_v = mb_detect_encoding($_v, 'UTF-8', TRUE) == 'UTF-8' ? $_v : utf8_encode($_v);
            $buff .= "{$indent}<{$k}>" . ($escape ? htmlspecialchars($_v) : $_v) . "</" . $k . ">\n";
          }
        }
      }
      else {
        $buff .= "{$indent}<{$k}>\n" . $this
          ->array2xml($v, $indent . "\t", $escape) . "{$indent}</" . $k . ">\n";
      }
    }
  }
  return $buff;
}