第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何將數(shù)組轉(zhuǎn)換為SimpleXML

如何將數(shù)組轉(zhuǎn)換為SimpleXML

飲歌長嘯 2019-06-01 16:50:10
如何將數(shù)組轉(zhuǎn)換為SimpleXML如何在PHP中將數(shù)組轉(zhuǎn)換為SimpleXML對象?
查看完整描述

3 回答

?
躍然一笑

TA貢獻1826條經(jīng)驗 獲得超6個贊

一個簡短的:

<?php

$test_array = array (
  'bla' => 'blub',
  'foo' => 'bar',
  'another_array' => array (
    'stack' => 'overflow',
  ),);$xml = new SimpleXMLElement('<root/>');array_walk_recursive($test_array, array ($xml, 'addChild'));print $xml->asXML();

結(jié)果

<?xml version="1.0"?><root>
  <blub>bla</blub>
  <bar>foo</bar>
  <overflow>stack</overflow></root>

鍵和值是交換的-您可以用array_flip()在數(shù)組行走之前。array_walk_recursive需要PHP 5。你可以用array_walk相反,但你不會'stack' => 'overflow'在XML中。


查看完整回答
反對 回復 2019-06-01
?
紫衣仙女

TA貢獻1839條經(jīng)驗 獲得超15個贊

下面是php5.2代碼,它將任意深度的數(shù)組轉(zhuǎn)換為XML文檔:

Array(
    ['total_stud']=> 500
    [0] => Array
        (
            [student] => Array
                (
                    [id] => 1
                    [name] => abc                    [address] => Array
                        (
                            [city]=>Pune
                            [zip]=>411006
                        )                       
                )
        )
    [1] => Array
        (
            [student] => Array
                (
                    [id] => 2
                    [name] => xyz                    [address] => Array
                        (
                            [city]=>Mumbai
                            [zip]=>400906
                        )   
                )

        ))

生成的XML如下:

<?xml version="1.0"?><student_info>
    <total_stud>500</total_stud>
    <student>
        <id>1</id>
        <name>abc</name>
        <address>
            <city>Pune</city>
            <zip>411006</zip>
        </address>
    </student>
    <student>
        <id>1</id>
        <name>abc</name>
        <address>
            <city>Mumbai</city>
            <zip>400906</zip>
        </address>
    </student></student_info>

PHP片段

<?php// function definition to convert array to xmlfunction array_to_xml( $data, &$xml_data ) {
    foreach( $data as $key => $value ) {
        if( is_numeric($key) ){
            $key = 'item'.$key; //dealing with <0/>..<n/> issues
        }
        if( is_array($value) ) {
            $subnode = $xml_data->addChild($key);
            array_to_xml($value, $subnode);
        } else {
            $xml_data->addChild("$key",htmlspecialchars("$value"));
        }
     }}// initializing or creating array$data = array('total_stud' => 500);
     // creating object of SimpleXMLElement$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');

// function call to convert array to xml
array_to_xml($data,$xml_data);

//saving generated xml file; 
$result = $xml_data->asXML('/file/path/name.xml');?>

關(guān)于.的文件SimpleXMLElement::asXML在這個片段中使用


查看完整回答
反對 回復 2019-06-01
  • 3 回答
  • 0 關(guān)注
  • 537 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號