AAA Главная
Примеры PHP Примеры JavaScript Примеры Ajax Примеры CSS,HTML

xml2array

Здесь собраны различные варианты ыфункция преобразования xml структуры в массив

$xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?>
<d:multistatus xmlns:d="DAV:"><d:response><d:href>/backup/</d:href>
<d:propstat><d:status>HTTP/1.1 200 OK</d:status>
<d:prop>
<d:resourcetype><d:collection/></d:resourcetype>
<d:getlastmodified>Fri, 26 Sep 2014 20:37:12 GMT</d:getlastmodified>
<d:displayname>backup</d:displayname><d:creationdate>2014-09-26T20:37:12Z</d:creationdate>
</d:prop>
</d:propstat></d:response><d:response><d:href>/backup/pngcrush.zip</d:href>
<d:propstat><d:status>HTTP/1.1 200 OK</d:status><d:prop><d:resourcetype/>
<d:getlastmodified>Fri, 26 Sep 2014 21:26:39 GMT</d:getlastmodified>
<d:getetag>6af2c93801e3ca8a3eb7e00a9763945b</d:getetag>
<d:getcontenttype>application/x-zip-compressed</d:getcontenttype>
<d:getcontentlength>139608</d:getcontentlength>
<d:displayname>pngcrush.zip</d:displayname><d:creationdate>2014-09-26T21:26:39Z</d:creationdate></d:prop>
</d:propstat></d:response></d:multistatus>');//, 'SimpleXMLElement', LIBXML_NOCDATA);
var_dump(xml2array($xml));

function xml2array($data) {
    if (is_object($data)) $data = get_object_vars($data);
    return (is_array($data)) ? array_map(__FUNCTION__,$data) : $data;
}
function xml2array($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();
    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }

    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = xml2array($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}
function xml2array($xml)
{
    $arr = array();

    foreach ($xml->children() as $r)
    {
        $t = array();
        if(count($r->children()) == 0)
        {
            $arr[$r->getName()] = strval($r);
        }
        else
        {
            $arr[$r->getName()][] = xml2array($r);
        }
    }
    return $arr;
}
function xml2array( $xmlObject, $out = array () )
{
    foreach ( (array) $xmlObject as $index => $node )
    {
        $out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
    }
    return $out;
}
function xml2array($xmlObject){
    $out = array();
    foreach($xmlObject->attributes() as $attr => $val)
        $out['@attributes'][$attr] = (string)$val;

    $has_childs = false;
    foreach($xmlObject as $index => $node)
    {
        $has_childs = true;
        $out[$index][] = xml2array($node);
    }
    if (!$has_childs && $val = (string)$xmlObject)
        $out['@value'] = $val;

    foreach ($out as $key => $vals)
    {
        if (is_array($vals) && count($vals) === 1 && array_key_exists(0, $vals))
            $out[$key] = $vals[0];
    }
    return $out;
}

.

© Copyright 2008-2016 by KDG