如何将SimpleXML对象转换为PHP数组?

时间:2011-10-15 15:41:15

标签: php arrays simplexml

请考虑以下代码:

$string = '<device>
    <id>1234</id>
    <label>118</label>
    <username>root</username>
    <password>helloWorld</password>
    <hardware>
        <memory>4GB RAM</memory>
        <storage_drives>
            <storage_drive_1>2TB SATA 7,200RPM</storage_drive_1>
            <storage_drive_2>1TB SATA 7,200RPM</storage_drive_2>
            <storage_drive_3>Not Applicable</storage_drive_3>
            <storage_drive_4>Not Applicable</storage_drive_4>
        </storage_drives>
    </hardware>
</device>';
$xml = new SimpleXMLElement($string);

$deviceDetails = Array();
foreach($xml as $element){
    $tag = $element->getName();
    $deviceDetails +=  Array($tag => '$element->$tag)',
        );
    }

输出$detailsDetails数组如下:

Array
(
    [id] => $element->$tag)
    [label] => $element->$tag)
    [username] => $element->$tag)
    [password] => $element->$tag)
    [hardware] => $element->$tag)
)

这是错误的。

我的问题是,如何让$element->$tag工作?

3 个答案:

答案 0 :(得分:16)

试试这个:

$string = '<device>
  <id>1234</id>
  <label>118</label>
  <username>root</username>
  <password>helloWorld</password>
  <hardware>
    <memory>4GB RAM</memory>
     <storage_drives>
      <storage_drive_1>2TB SATA 7,200RPM</storage_drive_1>
      <storage_drive_2>1TB SATA 7,200RPM</storage_drive_2>
      <storage_drive_3>Not Applicable</storage_drive_3>
      <storage_drive_4>Not Applicable</storage_drive_4>
    </storage_drives>
  </hardware>
</device>';

$xml = json_decode(json_encode((array) simplexml_load_string($string)), 1);

这将输出:

Array
(
    [id] => 1234
    [label] => 118
    [username] => root
    [password] => helloWorld
    [hardware] => Array
        (
            [memory] => 4GB RAM
            [storage_drives] => Array
                (
                    [storage_drive_1] => 2TB SATA 7,200RPM
                    [storage_drive_2] => 1TB SATA 7,200RPM
                    [storage_drive_3] => Not Applicable
                    [storage_drive_4] => Not Applicable
                )

        )

)

或者如果您不喜欢这样,您可以使用PHP类:http://www.bin-co.com/php/scripts/xml2array/

或查看 dfsq 回答

答案 1 :(得分:15)

Book of Zeus 代码包含在函数中以使其以递归方式工作:

function xml2array($xml)
{
    $arr = array();

    foreach ($xml as $element)
    {
        $tag = $element->getName();
        $e = get_object_vars($element);
        if (!empty($e))
        {
            $arr[$tag] = $element instanceof SimpleXMLElement ? xml2array($element) : $e;
        }
        else
        {
            $arr[$tag] = trim($element);
        }
    }

    return $arr;
}

$xml = new SimpleXMLElement($string);
print_r(xml2array($xml));

Array
(
    [id] => 1234
    [label] => 118
    [username] => root
    [password] => helloWorld
    [hardware] => Array
    (
        [memory] => 4GB RAM
        [storage_drives] => Array
        (
            [storage_drive_1] => 2TB SATA 7,200RPM
            [storage_drive_2] => 1TB SATA 7,200RPM
            [storage_drive_3] => Not Applicable
            [storage_drive_4] => Not Applicable
        )
    )
)

答案 2 :(得分:4)

试试这个:

$array = json_decode(json_encode((array)$xml), TRUE);