将阵列转换为XML时出现问题

时间:2013-02-21 13:03:49

标签: php xml arrays multidimensional-array

我有一个像这样的数组列表

    Array
(
    [351] => Array
        (
            [0] => Array
                (
                    [unit_id] => Array
                        (
                            [0] => 10192
                            [1] => 10192
                            [2] => 10192
                        )

                    [born_from] => Array
                        (
                            [0] => 1980-08-21
                        )

                    [born_to] => Array
                        (
                            [0] => 2010-08-18
                        )

                    [start_date] => Array
                        (
                            [0] => 2013-02-21 17:29:00
                        )

                    [sex] => Array
                        (
                            [0] => 3
                        )

                    [id] => 351
                    [product_id] => 1
                    [name] => test1
                )

        )

    [352] => Array
        (
            [0] => Array
                (
                    [unit_id] => Array
                        (
                            [0] => 10192
                            [1] => 10192
                            [2] => 10192
                        )

                    [born_from] => Array
                        (
                            [0] => 1985-01-01
                        )

                    [born_to] => Array
                        (
                            [0] => 2000-01-07
                        )

                    [sex] => Array
                        (
                            [0] => 2
                        )

                    [id] => 352
                    [product_id] => 2
                    [name] => Test2
                )

        )

)

现在将此数组转换为xml我有这段代码

$product_info = array($myPHPArray);

// creating object of SimpleXMLElement
$xml_product_info = new SimpleXMLElement("<?xml version=\"1.0\"?><product_info></product_info>");

// function call to convert array to xml
array_to_xml($product_info,$xml_product_info);

//saving generated xml file
$xml_product_info->asXML('C:/product.xml');


// function defination to convert array to xml
function array_to_xml($product_info, &$xml_product_info) {
    foreach($product_info as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml_product_info->addChild("$key");
                array_to_xml($value, $subnode);
            }
            else{
                array_to_xml($value, $xml_product_info);
            }
        }
        else {
            $xml_product_info->addChild("$key","$value");
        }
    }
}

这会给我这样的输出

<?xml version="1.0"?>
<product_info>
<unit_id>
    <0>10192</0>
    <1>10192</1>
    <2>10192</2>
</unit_id>
<born_from>
    <0>1980-08-21</0>
</born_from>
<born_to>
    <0>2010-08-18</0>
</born_to>
<individual_strdt>
    <0>2013-02-21 17:29:00</0>
</individual_strdt>
<elite_sex>
    <0>3</0>
</elite_sex>
<id>351</id>
<product_id>1</product_id>
<name>test1</name>

// here it stars for 352
<unit_id>
    <0>10192</0>
    <1>10192</1>
    <2>10192</2>
</unit_id>
.
.
.
.
</product_info>

这里

1)我无法识别特定元素的值351352 它应该

<?xml version="1.0"?>
<product_info>
    <351>
    //data of 351
    </351>
    <352>
    //data of 352
    </352>
</product_info>

2)当一个特定的子数组转换为xml时,它会存储<0>1092</0>的数据,就像那样会增加我最终文件的大小

3)我可以将所有这个子数组的值用作属性??

4)安排此数据的任何其他智能方法

我是xml的新手,所以欢迎任何新想法

0 个答案:

没有答案