You are here

function array2xml in Campaign Monitor 5.2

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:

string $indent (Optional) Starting identation of each element:

string $escape (Optional) Determines whether or not to escape a text node.:

Return value

string An XML fragment.

1 call to 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 820

Code

function array2xml($arr, $indent = '', $escape = true) {
  $buff = '';
  foreach ($arr as $k => $v) {
    if (!is_array($v)) {
      $buff .= "{$indent}<{$k}>" . ($escape ? utf8_encode($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" . array2xml($_v, $indent . "\t", $escape) . "{$indent}</{$k}>\n";
          }
          else {
            $buff .= "{$indent}<{$k}>" . ($escape ? utf8_encode($_v) : $_v) . "</{$k}>\n";
          }
        }
      }
      else {
        $buff .= "{$indent}<{$k}>\n" . array2xml($v, $indent . "\t", $escape) . "{$indent}</{$k}>\n";
      }
    }
  }
  return $buff;
}