note 126032 added to simplexmlelement.addchild

From: Date: Thu, 22 Apr 2021 13:23:45 +0000
Subject: note 126032 added to simplexmlelement.addchild
Groups: php.notes 
Request: Send a blank email to php-notes+get-216314@lists.php.net to get a copy of this message
I create this function so you can through and array build the XML, this allows multiple keys with the same value. The structure must follow [ 'key' => 'nameOfTheKey', 'attributes' => ['id' => '1'], 'fields' => [ ['key' => 'fieldSamName', 'value' => '1'], ['key' => 'fieldSamName', 'value' => '1'], ['key' => 'fieldSamName', 'value' => '2'], ['key' => 'fieldWithChild', 'fields' => [ ['key' => 'car', 'value' => '2'], ['key' => 'human', 'value' => '3'], ]], ] ]; Function: function toXml(\SimpleXMLElement $node, array $data) { foreach ($data as $key => $value) { $key = $value['key']; if (isset($value['value'])) { $newNode = $node->addChild($key, $value['value']); if (isset($value['attributes'])) { foreach ($value['attributes'] as $keyAttr => $valueAttr) { $newNode->addAttribute($keyAttr, $valueAttr); } } } else { $newNode = $node->addChild($key); if (isset($value['attributes'])) { foreach ($value['attributes'] as $keyAttr => $valueAttr) { $newNode->addAttribute($keyAttr, $valueAttr); } } } if (isset($value['fields'])) { foreach ($value['fields'] as $keyField => $valueField) { $this->toXml($newNode, [$keyField => $valueField]); } } } } ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 177.23.116.129) ---- Manual Page -- https://php.net/manual/en/simplexmlelement.addchild.php Edit -- https://main.php.net/note/edit/126032 Del: integrated -- https://main.php.net/note/delete/126032/integrated Del: useless -- https://main.php.net/note/delete/126032/useless Del: bad code -- https://main.php.net/note/delete/126032/bad+code Del: spam -- https://main.php.net/note/delete/126032/spam Del: non-english -- https://main.php.net/note/delete/126032/non-english Del: in docs -- https://main.php.net/note/delete/126032/in+docs Del: other reasons-- https://main.php.net/note/delete/126032 Reject -- https://main.php.net/note/reject/126032 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#216314) next »