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

Public Member Functions

 xmlToAssoc (\SimpleXMLElement $xml)
 

Detailed Description

Convert xml data (SimpleXMLElement object) to array

Definition at line 11 of file Xml.php.

Member Function Documentation

◆ xmlToAssoc()

xmlToAssoc ( \SimpleXMLElement  $xml)

Transform \SimpleXMLElement to associative array \SimpleXMLElement must be conform structure, generated by assocToXml()

Parameters
\SimpleXMLElement$xml
Returns
array

Definition at line 20 of file Xml.php.

21  {
22  $array = [];
23  foreach ($xml as $key => $value) {
24  if (isset($value->{$key})) {
25  $i = 0;
26  foreach ($value->{$key} as $v) {
27  $array[$key][$i++] = (string)$v;
28  }
29  } else {
30  // try to transform it into string value, trimming spaces between elements
31  $array[$key] = trim((string)$value);
32  if (empty($array[$key]) && !empty($value)) {
33  $array[$key] = $this->xmlToAssoc($value);
34  } else {
35  // untrim strings values
36  $array[$key] = (string)$value;
37  }
38  }
39  }
40  return $array;
41  }
$value
Definition: gender.phtml:16
xmlToAssoc(\SimpleXMLElement $xml)
Definition: Xml.php:20
$i
Definition: gallery.phtml:31

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