PHP:出口阵列XML麻烦 [英] php: export array to xml trouble

查看:119
本文介绍了PHP:出口阵列XML麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,我需要转换为XML用SimpleXML。下面的方法几乎是在做的工作,但有一件麻烦了。它不能产生结构是这样的:

  $ xmlFields =阵列(
        rootElt'=>阵列(
            字段1'=> '',
            '字段2'=> '',
            FIELD3'=>阵列(
                '字段4 =>阵列(
                    收入= GT;阵列(
                        所有者= GT; '',
                        '描述'=> '',
                    )
                    收入= GT;阵列(
                        所有者= GT; '',
                        '描述'=> '',
                    )
                )
            )
        )
    );

这仅写入部分字段4的最后收​​入,但我需要一个像输出:

 <&字段4 GT;
    <收入及GT;
        <所有者GT; ....< /所有者>
        <描述> ....< /描述>
    < /收入>
    <收入及GT;
        <所有者GT; ....< /所有者>
        <描述> ....< /描述>
    < /收入>
< /字段4>

有人能帮助我解决这个功能:

  / **
 * @参数数组$ dataArr
 * @参数的SimpleXMLElement $ xmlObj
 * /
私有函数array2xml($ dataArr,$ xmlObj){
    的foreach($ dataArr为$关键=> $值){
        如果(is_array($值)){
            如果(!is_numeric($键)){
                $子节点= $ xmlObj->的addChild($键);
                自:: array2xml(价值$,$子节点);
            }其他{
                自:: array2xml(价值$,$ xmlObj);
            }
        }其他{
            $ xmlObj->的addChild($键,$值);
        }
    }
}


解决方案

在code看起来不错SimpleXML中的条款,但你有一个误解数组在PHP中是如何工作的:

 '字段4 =>阵列(
    收入= GT;阵列(
        所有者= GT; '',
        '描述'=> '',
    )
    收入= GT;阵列(
        所有者= GT; '',
        '描述'=> '',
    )

这不加入两个值的字段4 数组,但之一。请参见 http://php.net/array ,例如例2 的。在收入的第二个定义键取代了第一位。

您的可能是什么的想在这种情况下,要做的就是使用不同的结构与阵列:

 '字段4 =>阵列(
    阵列('收入'=>阵列(
        所有者= GT; '',
        '描述'=> '',
    ))
    阵列('收入'=>阵列(
        所有者= GT; '',
        '描述'=> '',
    ))

如果你把它包装自己的数组元素中的每个元素,你的code甚至会不会有什么特殊情况,因为每个元素都是相同的。然而,这将意味着你会 - 如果你手动创建阵列 - 写更多code来定义它。

我想到的另一种方法是这样的:

 '字段4 =>阵列(
    收入= GT;阵列(
        阵列(
            所有者= GT; '',
            '描述'=> '',
        )
        阵列(
            所有者= GT; '',
            '描述'=> '',
        )
    )

这仍然不是你允许有相同的元素名称的多个组,但写这将是可能更容易。

有关后来的阵列结构(我认为这是你想用什么),我创建了一个simplexml的阵列进口商/转换器(的要点)。用法是pretty直线前进在它的基本形式,但你可以用它做更多差异化的东西:

  / *例1:创建基于阵列上一个新的SimpleXMLElement * /$进口=新SimpleXMLArrayImport();$ XML = $导入 - > importArray(['根'=>'']);/ *<?XML版本=1.0>
 *<根/> * // *例2:添加一个空的子元素为根* /$新= $导入 - > addArray($ XML,['数字'=>'']);/ *<?XML版本=1.0>
 *<根和GT;
 *<数字/>
 *< /根> * // *实施例3:添加同名元素的列表的根* /$持续= $导入 - > addArray($新,['编号'= GT; [0,1,42]]);/ *<?XML版本=1.0>
 *<根和GT;
 *<数字>
 * LT;数大于0&下; /数>
 * LT;数大于1&下; /数>
 *<&号GT; 42℃; /数字>
 *< /数字>
 *< /根> * // *例4:最后的添加元素设置属性* /$最后['注'] ='的答案,以生命,宇宙和万物的终极问题;/ *<?XML版本=1.0>
 *<根和GT;
 *<数字>
 * LT;数大于0&下; /数>
 * LT;数大于1&下; /数>
 *<数注=答案的......> 42℃; /数字>
 *< /数字>
 *< /根> * // *例5:创建一个完整的文档* /$ xmlFields =阵列(
    rootElt'=>阵列(
        字段1'=> '',
        '字段2'=> '',
        FIELD3'=>阵列(
            '字段4 =>阵列(
                收入= GT;阵列(
                    阵列(
                        所有者= GT; '',
                        '描述'=> '',
                    )
                    阵列(
                        所有者= GT; '',
                        '描述'=> '',
                    )
                )
            )
        )
    )
);$进口=新SimpleXMLArrayImport($ xmlFields);$ XML = $导入 - > getDocument(); #在SimpleXML的根元素/ *<?XML版本=1.0>
 *< rootElt>
 *<字段1 />
 *<场2 />
 *<&FIELD3 GT;
 *<&字段4 GT;
 *<收入及GT;
 *<所有者/>
 *<描述/>
 *< /收入>
 *<收入及GT;
 *<所有者/>
 *<描述/>
 *< /收入>
 *< /字段4>
 *< / FIELD3>
 *< / rootElt> * /

I have an array which I need to convert to XML with SimpleXML. The method below is almost doing the work but there's one trouble with it. It can't generate structure like this:

    $xmlFields = array(  
        'rootElt' => array(
            'field1' => '',
            'field2' => '',                
            'field3' => array(                  
                'field4'  => array(
                    'income' => array(
                        'owner' => '',                            
                        'description' => '', 
                    ),
                    'income' => array(
                        'owner' => '',                            
                        'description' => '', 
                    ),
                ),
            )
        )
    );

It writes only the last 'income' of section 'field4' but I need the output like:

<field4>
    <income>
        <owner>....</owner>            
        <description>....</description>
    </income>
    <income>
        <owner>....</owner>            
        <description>....</description>
    </income>
</field4>

Could someone help me fix this function:

/**     
 * @param array $dataArr
 * @param SimpleXMLElement $xmlObj 
 */
private function array2xml( $dataArr, $xmlObj ) {
    foreach ( $dataArr as $key => $value ) {          
        if ( is_array($value) ) {                        
            if ( !is_numeric($key) ) {                    
                $subnode = $xmlObj->addChild( $key );                    
                self::array2xml( $value, $subnode );                    
            } else {                                        
                self::array2xml( $value, $xmlObj );      
            }
        } else {
            $xmlObj->addChild( $key, $value );                       
        }
    }
}

解决方案

The code looks fine in terms of SimpleXML, however you have a misunderstanding of how arrays work in PHP:

'field4'  => array(
    'income' => array(
        'owner' => '',                            
        'description' => '', 
    ),
    'income' => array(
        'owner' => '',                            
        'description' => '', 
    ),

That is not adding two values to the field4 array, but one. See http://php.net/array, for example Example #2. The second definition of the income key replaces the first one.

What you might want to do in this case is to use a different structure with your array:

'field4'  => array(
    array('income' => array(
        'owner' => '',                            
        'description' => '', 
    )),
    array('income' => array(
        'owner' => '',                            
        'description' => '', 
    )),

If you wrap each element inside an array element it's own, your code would even not have any special cases because each element is the same. However this would mean that you would - if you create the array by hand - write more code to define it.

Another alternative that comes to mind would be this:

'field4'  => array(
    'income' => array(
        array(
            'owner' => '',                            
            'description' => '', 
        ),
        array(
            'owner' => '',                            
            'description' => '', 
        ),            
    ),

This still wouldn't you allow to have multiple groups with the same element name, but writing it would be probably easier.

For the later array structure (I think that's what you wanted to use), I've created a simplexml array importer / converter (Gist). The usage is pretty straight forward in it's basic form, but you can do even more differentiated stuff with it:

/* Example 1: Create a new SimpleXMLElement based on an array */

$import = new SimpleXMLArrayImport();

$xml = $import->importArray(['root' => '']);

/* <?xml version="1.0"?>
 * <root/>                                                    */

/* Example 2: Add an empty child element to the root          */

$new = $import->addArray($xml, ['numbers' => '']);

/* <?xml version="1.0"?>
 * <root>
 *   <numbers/>
 * </root>                                                    */

/* Example 3: Add a list of same-named elements to the root   */

$last = $import->addArray($new, ['number' => [0, 1, 42]]);

/* <?xml version="1.0"?>
 * <root>
 *     <numbers>
 *         <number>0</number>
 *         <number>1</number>
 *         <number>42</number>
 *     </numbers>
 * </root>                                                    */

/* Example 4: Set attribute of last added element             */

$last['note'] = 'The Answer to the Ultimate Question of Life, the Universe, and Everything';

/* <?xml version="1.0"?>
 * <root>
 *   <numbers>
 *     <number>0</number>
 *     <number>1</number>
 *     <number note="The Answer to the ...">42</number>
 *   </numbers>
 * </root>                                                    */

/* Example 5: Create a full document                          */

$xmlFields = array(
    'rootElt' => array(
        'field1' => '',
        'field2' => '',
        'field3' => array(
            'field4' => array(
                'income' => array(
                    array(
                        'owner'       => '',
                        'description' => '',
                    ),
                    array(
                        'owner'       => '',
                        'description' => '',
                    ),
                ),
            ),
        )
    )
);

$import = new SimpleXMLArrayImport($xmlFields);

$xml = $import->getDocument(); # The SimpleXML Root Element

/* <?xml version="1.0"?>
 * <rootElt>
 *   <field1/>
 *   <field2/>
 *   <field3>
 *     <field4>
 *       <income>
 *         <owner/>
 *         <description/>
 *       </income>
 *       <income>
 *         <owner/>
 *         <description/>
 *       </income>
 *     </field4>
 *   </field3>
 * </rootElt>                                                 */

这篇关于PHP:出口阵列XML麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆