Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions
ConvertArray Class Reference

Public Member Functions

 assocToXml (array $array, $rootName='_')
 

Static Public Member Functions

static toFlatArray ($data)
 

Detailed Description

Convert the array data to SimpleXMLElement object

Definition at line 14 of file ConvertArray.php.

Member Function Documentation

◆ assocToXml()

assocToXml ( array  $array,
  $rootName = '_' 
)

Transform an assoc array to \SimpleXMLElement object Array has some limitations. Appropriate exceptions will be thrown

Parameters
array$array
string$rootName
Returns
\SimpleXMLElement
Exceptions
LocalizedException

Definition at line 25 of file ConvertArray.php.

26  {
27  if (empty($rootName) || is_numeric($rootName)) {
28  throw new LocalizedException(
29  new \Magento\Framework\Phrase(
30  "The root element can't be empty or use numbers. Change the element and try again."
31  )
32  );
33  }
34 
35  $xmlStr = <<<XML
36 <?xml version='1.0' encoding='UTF-8' standalone='yes'?>
37 <$rootName></$rootName>
38 XML;
39  $xml = new \SimpleXMLElement($xmlStr);
40  foreach (array_keys($array) as $key) {
41  if (is_numeric($key)) {
42  throw new LocalizedException(
43  new \Magento\Framework\Phrase('An error occurred. Use non-numeric array root keys and try again.')
44  );
45  }
46  }
47  return self::_assocToXml($array, $rootName, $xml);
48  }

◆ toFlatArray()

static toFlatArray (   $data)
static

Convert nested array into flat array.

Parameters
array$data
Returns
array

Definition at line 56 of file ConvertArray.php.

57  {
58  foreach ($data as $key => $value) {
59  if (is_array($value)) {
61  unset($data[$key]);
62  $data = array_merge($data, $value);
63  }
64  }
65  return $data;
66  }
$value
Definition: gender.phtml:16

The documentation for this class was generated from the following file: