数组转xhl

浏览:14 发布日期:2020/02/21 分类:功能实现 关键字: xhl 数组
数组转xhl
<?php
$a= Array(
    'userId' => '123',
    'rptDate' => '2019-01-22',
    'datalist' => Array('0' => Array (
                    'code' => 33,
                    'buyOrder' => 44,
                    'saleOrder' => 123.00,
                    'name' => '西侯村',
                    'countyType' => 3,
                    'detail' => Array(
                            '0' => Array(
                                    'comm_id' => 44,
                                    'money' => 3.00,
                                ),

                            '1' => Array(
                                    'comm_id' => 16,
                                    'money' => 23.00,
                                )

                        )

                ),

            '1' => Array(
                    'code' => 34,
                    'buyOrder' => 12323,
                    'saleOrder' => 3.00,
                    'name' => '杨柳村',
                    'countyType' => 2,
                    'detail' => Array(
                            '0' => Array(
                                    'comm_id' => 33,
                                    'money' => 123.00,
                                ),

                            '1' => Array(
                                    'comm_id' => 38,
                                    'money' => 33.00,
                                ),

                            '2' => Array(
                                    'comm_id' => 14,
                                    'money' => 13.00,
                                )

                        )

                )
,
            '2' => Array
                (
                    'code' => 2323,
                    'buyOrder' => 43,
                    'saleOrder' => 34343.00,
                    'name' => '魏庄南村',
                    'countyType' => 3,
                    'detail' => Array(
                            '0' => Array(
                                    'comm_id' => 9,
                                    'money' => 33.00,
                                )
,
                            '1' => Array(
                                    'comm_id' => 32,
                                    'money' => 3232.00,
                                )
,
                            '2' => Array(
                                    'comm_id' => 4,
                                    'money' => 123.00,
                                )

                        )

                )

        )

);

/**
*   将数组转换为xml
*    @param array $data    要转换的数组
*   @param bool $root     是否要根节点
*   @return string         xml字符串
*    @author Dragondean
*    @url    http://www.cnblogs.com/dragondean
*/
function arr2xml($data, $root = true){
    $str="";
    if($root)$str .= "<xml>";
    foreach($data as $key => $val){
        if(is_array($val)){
            $child = arr2xml($val, false);
            $str .= " $child ";
        }else if(is_int($key)){
            $str.=  $val ;
        }else if($key=='datalist'){
            $str.= $val;
        }else{
            $str.= "<$key>".$val."</$key>";
        }
    }
    if($root)$str .= "</xml>";
    return $str;
}
$xmlrel=arr2xml($a);
echo($xmlrel);

var_dump($a);
评论( 相关
后面还有条评论,点击查看>>