Looping through a SimpleXML object, or turning the whole thing into an array

五迷三道 提交于 2019-11-27 20:39:10
Tim Lytle

You can use the SimpleXML object (or its properties) in a foreach loop. If you want to loop through all the 'records' something like this can be used to access and display the data:

//Loop through all the members of the Item array 
//(essentially your two database rows).
foreach($SimpleXML->body->QueryWithAttributesResult->Item as $Item){
    //Now you can access the 'row' data using $Item in this case 
    //two elements, a name and an array of key/value pairs
    echo $Item->Name;
    //Loop through the attribute array to access the 'fields'.
    foreach($Item->Attribute as $Attribute){
        //Each attribute has two elements, name and value.
        echo $Attribute->Name . ": " . $Attribute->Value;
    }
}

Note that $Item will be a SimpleXML object, as is $Attribute, so they need to be referenced as objects, not arrays.

While the example code above is looping through the arrays in the SimpleXML object ($SimpleXML->body->QueryWithAttributesResult->Item), you can also loop through a SimpleXML object (say $SimpleXML->body->QueryWithAttributesResult->Item[0]), and that would give you each of the object's properties.

Each child element of a SimpleXML object is an XML entity. If the XML entity (tag) is not unique, then the element is simply an array of SimpleXML objects representing each entity.

If you want, this should create a more common row/fields array from your SimpleXML object (or get you close):

foreach($SimpleXML->body->QueryWithAttributesResult->Item as $Item){
    foreach($Item->Attribute as $Attribute){
        $rows[$Item->Name][$Attribute->Name] = $Attribute->Value;
    }
}

//Now you have an array that looks like:
$rows['message12413344443260']['active'] = 1;
$rows['message12413344443260']['user'] = 'john';
//etc.
Sebastian
get_object_vars($simpleXMLElement);

one slight addition for for the PHP 5.2 fix.

$response_array = json_decode(json_encode($response),true);

In the case of XML responses that DON'T contain CDATA sections (like Amazon's/Tarzan's), you can use the following assuming you have PHP 5.2 or newer.

// Get a SimpleXML response back from Tarzan
$s3 = new AmazonS3();
$response = $s3->list_buckets();

// Convert SimpleXML to Array in PHP 5.2.
$response_array = json_decode(json_encode($response));

This will be a standard utility available to all objects in the next major version of Tarzan (CloudFusion 2.5).

Almost all examples I found refer to a specific example where you already know the nodes. Following function will convert a SimpleXMLElement into an array without knowing Node Names or if they have childs or not. Will also work fine with CDATA Sections.

function parseXML(SimpleXMLElement $xml) : array
{
    $array = [];

    foreach(iterator_to_array($xml, TRUE) as $key => $value)
        $array[$key] = ($value->count() > 1) ? parseXML($value) : (string)$value;

    return $array;
}

This worked:

// $result is a Simple XML object

$result = convertSimpleXMLToArray($result);

function convertSimpleXMLToArray($xml)
{
    if(class_basename($xml) == 'SimpleXMLElement')
    {
        $xml = get_object_vars($xml);
        foreach($xml as $key => $value)
        {
            if(class_basename($value) == 'SimpleXMLElement') $xml[$key] = convertSimpleXMLToArray($value);
        }
    }

    return $xml;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!