note 112420 added to class.simplexmlelement

From: Date: Fri, 14 Jun 2013 13:36:30 +0000
Subject: note 112420 added to class.simplexmlelement
Groups: php.notes 
Request: Send a blank email to php-notes+get-195224@lists.php.net to get a copy of this message
Important note I miss here in the documentation: SimpleXML supports Array/Iteration-Methods. Therefore it is possible to add attributes edit attributes remove attributes add nodes edit nodes remove nodes This is the reason why SimpleXML provides only add-methods not deleting- or editing-methods. We also need this methods because SimpleXML acts as a normal class and new member will not converted to a new node. Deleting a node seems not to be possible within a foreach-loop. The reason is simple. To do that we need a valid key, but the Iterator only gives us a "understandable feedback" on which node we are working on: the tag name. So this will not work: <?php foreach($doc->seg as $key => $seg) { if((string)$seg['id'] === 'whatever') { unset($seg); // only clears local copy unset($seg[$key]); // wrong index "seg" } } ?> But this, eg. will work: <?php unset($doc->seg[2]); ?> In case of doubt always do a print_r/var_dump(). It nicely shows the real linked indexes. ---- Server IP: 213.172.110.14 Probable Submitter: 84.112.29.91 ---- Manual Page -- http://php.net/manual/en/class.simplexmlelement.php Edit -- https://master.php.net/note/edit/112420 Del: integrated -- https://master.php.net/note/delete/112420/integrated Del: useless -- https://master.php.net/note/delete/112420/useless Del: bad code -- https://master.php.net/note/delete/112420/bad+code Del: spam -- https://master.php.net/note/delete/112420/spam Del: non-english -- https://master.php.net/note/delete/112420/non-english Del: in docs -- https://master.php.net/note/delete/112420/in+docs Del: other reasons-- https://master.php.net/note/delete/112420 Reject -- https://master.php.net/note/reject/112420 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195224) next »