List Info

Thread: note 66896 added to ref.simplexml




note 66896 added to ref.simplexml
user name
2006-05-31 17:31:23
Here is my Simple XML to array function, it is recursive and
has the benefit of maintaining key value relationships and
has worked well for me.

function XMLToArray($xml)
{
  if ($xml instanceof SimpleXMLElement) {
    $children = $xml->children();
    $return = null;
  }

  foreach ($children as $element => $value) {
    if ($value instanceof SimpleXMLElement) {
      $values = (array)$value->children();
      
      if (count($values) > 0) {
        $return[$element] = XMLToArray($value);
      } else {
        if (!isset($return[$element])) {
          $return[$element] = (string)$value;
        } else {
          if (!is_array($return[$element])) {
            $return[$element] = array($return[$element],
(string)$value);
          } else {
            $return[$element][] = (string)$value;
          }
        }
      }
    }
  }
  
  if (is_array($return)) {
    return $return;
  } else {
    return $false;
  }
}
----
Server IP: 64.71.164.2
Probable Submitter: 216.127.92.69
----
X-Spam-Status: No, hits=3.1 required=5.0
tests=DATE_MISSING,FROM_NO_LOWER 
	autolearn=no version=2.64
----
Manual Page -- http:/
/www.php.net/manual/en/ref.simplexml.php
Edit        -- http://master.p
hp.net/note/edit/66896
Del: integrated  -- ht
tp://master.php.net/note/delete/66896/integrated
Del: useless     -- http:
//master.php.net/note/delete/66896/useless
Del: bad code    -- http
://master.php.net/note/delete/66896/bad+code
Del: spam        -- http://m
aster.php.net/note/delete/66896/spam
Del: non-english -- h
ttp://master.php.net/note/delete/66896/non-english
Del: in docs     -- http:
//master.php.net/note/delete/66896/in+docs
Del: other reasons-- http://master
.php.net/note/delete/66896
Reject      -- http://master
.php.net/note/reject/66896
Search      -- http://ma
ster.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )