Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConvertArray.php
Go to the documentation of this file.
1 <?php
8 
10 
15 {
25  public function assocToXml(array $array, $rootName = '_')
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  }
49 
56  public static function toFlatArray($data)
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  }
67 
77  private function _assocToXml(array $array, $rootName, \SimpleXMLElement $xml)
78  {
79  $hasNumericKey = false;
80  $hasStringKey = false;
81  foreach ($array as $key => $value) {
82  if (!is_array($value)) {
83  if (is_string($key)) {
84  if ($key === $rootName) {
85  throw new LocalizedException(
86  new \Magento\Framework\Phrase(
87  "An associative key can't be the same as its parent associative key. "
88  . "Verify and try again."
89  )
90  );
91  }
92  $hasStringKey = true;
93  $xml->addChild($key, $value);
94  } elseif (is_int($key)) {
95  $hasNumericKey = true;
96  $xml->addChild($key, $value);
97  }
98  } else {
99  $xml->addChild($key);
100  self::_assocToXml($value, $key, $xml->{$key});
101  }
102  }
103  if ($hasNumericKey && $hasStringKey) {
104  throw new LocalizedException(
105  new \Magento\Framework\Phrase(
106  "Associative and numeric keys can't be mixed at one level. Verify and try again."
107  )
108  );
109  }
110  return $xml;
111  }
112 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16
assocToXml(array $array, $rootName='_')