attributes()) { foreach ($element->attributes() as $name => $value) { $array[$element->getName() . '_' . $name] = (string)$value; } if (trim((string)$element) !== '') { $array[$element->getName()] = trim((string)$element); } } // Handle child elements $items = []; foreach ($element->children() as $child) { $value = ($child->count() > 0 || $child->attributes()) ? self::xmlToArray($child) : (string)$child; if ($child->getName() === 'item') { $items[] = $value; } else if (isset($array[$child->getName()])) { if (!is_array($array[$child->getName()])) { $array[$child->getName()] = [$array[$child->getName()]]; } /** @noinspection UnsupportedStringOffsetOperationsInspection */ $array[$child->getName()][] = $value; } else { $array[$child->getName()] = $value; } } if (!empty($items)) { $array['item'] = $items; } return $array; } }