亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

php 怎么將數組轉xml的函數?

php 怎么將數組轉xml的函數?

PHP C
眼眸繁星 2019-05-22 15:15:59
php 怎么將數組轉xml的函數
查看完整描述

4 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

沒有現成函數,只能自己寫;我有一個別人寫的函數:
<?php
class A2Xml {
private $version = '1.0';
private $encoding = 'UTF-8';
private $root = 'root';
private $xml = null;
function __construct() {
$this->xml = new XmlWriter();
}
function toXml($data, $eIsArray=FALSE) {
if(!$eIsArray) {
$this->xml->openMemory();
$this->xml->startDocument($this->version, $this->encoding);
$this->xml->startElement($this->root);
}
foreach($data as $key => $value){

if(is_array($value)){
$this->xml->startElement($key);
$this->toXml($value, TRUE);
$this->xml->endElement();
continue;
}
$this->xml->writeElement($key, $value);
}
if(!$eIsArray) {
$this->xml->endElement();
return $this->xml->outputMemory(true);
}
}
}
$res = array(
'hello' => '11212',
'world' => '232323',
'array' => array(
'test' => 'test',
'b' => array('c'=>'c', 'd'=>'d')
),
'a' => 'haha'
);
$xml = new A2Xml();
echo $xml->toXml($res);



查看完整回答
反對 回復 2019-05-26
?
弒天下

TA貢獻1818條經驗 獲得超8個贊

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

<?php

class A2Xml {

  private $version  = '1.0';

  private $encoding  = 'UTF-8';

  private $root    = 'root';

  private $xml    = null;

  function __construct() {

    $this->xml = new XmlWriter();

  }

  function toXml($data, $eIsArray=FALSE) {

    if(!$eIsArray) {

      $this->xml->openMemory();

      $this->xml->startDocument($this->version, $this->encoding);

      $this->xml->startElement($this->root);

    }

    foreach($data as $key => $value){

   

      if(is_array($value)){

        $this->xml->startElement($key);

        $this->toXml($value, TRUE);

        $this->xml->endElement();

        continue;

      }

      $this->xml->writeElement($key, $value);

    }

    if(!$eIsArray) {

      $this->xml->endElement();

      return $this->xml->outputMemory(true);

    }

  }

}

$res = array(

  'hello' => '11212',

  'world' => '232323',

  'array' => array(

    'test' => 'test',

    'b'  => array('c'=>'c', 'd'=>'d')

  ),

  'a' => 'haha'

);

$xml = new A2Xml();

echo $xml->toXml($res);


查看完整回答
反對 回復 2019-05-26
?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

xml轉array方法沒錯,只是xml中有三個<list>,而數組中卻不能出現三個$arr['list'],所以這個方法自動把三個<list>中的內容放進了一個二維數組中。
可以嘗試直接取$arr['list'],取出結果應該就是 Array ( [0] => 1 [1] => 2 [2] => 3 ) 了。

查看完整回答
反對 回復 2019-05-26
?
Helenr

TA貢獻1780條經驗 獲得超4個贊

public function arrayToXml($arr){
$xml = "<xml>";
foreach ($arr as $key=>$val){
if(is_array($val)){
$xml.="<".$key.">".arrayToXml($val)."</".$key.">";
}else{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
}
$xml.="</xml>";
return $xml;
}

查看完整回答
反對 回復 2019-05-26
  • 4 回答
  • 0 關注
  • 1251 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號