XML到MD-Array

时间:2017-06-24 11:36:59

标签: php arrays xml

我正在尝试将XML Feed转换为Array

XML

<?xml version="1.0" encoding="utf-8" ?>
<products>
    <auction>
        <uniqueid>360116 05</uniqueid>
        <title>puma</title>
        <category>men / puma</category>
        <description></description>
        <price>111.00</price>
        <currency>eur</currency>
        <brand>puma</brand>
        <photos>
            <url>http://via.placeholder.com/350x150?text=1</url>
            <url>http://via.placeholder.com/350x150?text=2</url>
            <url>http://via.placeholder.com/350x150?text=3</url>
            <url>http://via.placeholder.com/350x150?text=4</url>
            <url>http://via.placeholder.com/350x150?text=5</url>
            <url>http://via.placeholder.com/350x150?text=6</url>
        </photos>
        <stocks>
                <stock>
                    <amount>1</amount>
                    <size>44</size>
                </stock>
                <stock>
                    <amount>1</amount>
                    <size>43</size>
                </stock>
                <stock>
                    <amount>1</amount>
                    <size>42</size>
                </stock>
        </stocks>
    </auction>
</products>

我使用此函数将XML转换为数组,但效果不如我所料。 例如,有6个URL,当它转换为数组时,它只有一个URL。 aare不能正常工作

protected function xml2array($xml) {
    $temp = [];

    foreach ($xml as $k => $element) {
        $tag = $element->getName();
        $e = get_object_vars($element);

        if (!empty($e))
            $temp[$tag] = $element instanceof SimpleXMLElement ? $this->xml2array($element) : $e;
        else
            $temp[$tag] = trim($element);
    }

    return $temp;
}

转换后

Array
(
    [auction] => Array
        (
            [uniqueid] => 360116 05
            [title] => puma
            [category] => men / puma
            [description] => 
            [price] => 111.00
            [currency] => eur
            [brand] => puma
            [photos] => Array
                (
                    [url] => http://via.placeholder.com/350x150?text=6
                )

            [stocks] => Array
                (
                    [stock] => Array
                        (
                            [amount] => 1
                            [size] => 42
                        )

                )

        )

)

如何在数组中添加所有网址和股票?

2 个答案:

答案 0 :(得分:2)

将XML解析为数组的最简单方法是json_encode()SimpleXMLElement返回的simplexml_load_string()对象,然后json_decode() JSON,传递TRUE作为第二个论点:

$xml = <<< END
<?xml version="1.0" encoding="utf-8" ?>
<products>
    <!-- your XML here -->
</products>
END;

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

在这里查看输出:https://3v4l.org/nKKDn

答案 1 :(得分:0)

将您的代码更改为

if (!empty($e)) {
    $temp[$tag] = $element instanceof SimpleXMLElement ? $this->xml2array($element) : $e;
} elseif (array_key_exist($tag, $temp)) {
    if (!is_array($temp[$tag])) {
        $temp[$tag] = array($temp[$tag]);
    }
    $temp[$tag][] = trim($element);
} else {
    $temp[$tag] = trim($element);
}