note 122006 added to class.simplexmlelement

From: Date: Mon, 11 Dec 2017 10:48:33 +0000
Subject: note 122006 added to class.simplexmlelement
Groups: php.notes 
Request: Send a blank email to php-notes+get-209348@lists.php.net to get a copy of this message
Map xml to array (with attributes) <?php declare(strict_types=1); /** * @param SimpleXMLElement $xml * @return array */ function xmlToArray(SimpleXMLElement $xml): array { $parser = function (SimpleXMLElement $xml, array $collection = []) use (&$parser) { $nodes = $xml->children(); $attributes = $xml->attributes(); if (0 !== count($attributes)) { foreach ($attributes as $attrName => $attrValue) { $collection['attributes'][$attrName] = strval($attrValue); } } if (0 === $nodes->count()) { $collection['value'] = strval($xml); return $collection; } foreach ($nodes as $nodeName => $nodeValue) { if (count($nodeValue->xpath('../' . $nodeName)) < 2) { $collection[$nodeName] = $parser($nodeValue); continue; } $collection[$nodeName][] = $parser($nodeValue); } return $collection; }; return [ $xml->getName() => $parser($xml) ]; } ---- Server IP: 208.43.231.9 Probable Submitter: 14.202.210.85 ---- Manual Page -- http://php.net/manual/en/class.simplexmlelement.php Edit -- https://master.php.net/note/edit/122006 Del: integrated -- https://master.php.net/note/delete/122006/integrated Del: useless -- https://master.php.net/note/delete/122006/useless Del: bad code -- https://master.php.net/note/delete/122006/bad+code Del: spam -- https://master.php.net/note/delete/122006/spam Del: non-english -- https://master.php.net/note/delete/122006/non-english Del: in docs -- https://master.php.net/note/delete/122006/in+docs Del: other reasons-- https://master.php.net/note/delete/122006 Reject -- https://master.php.net/note/reject/122006 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#209348) next »