中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

php中simplexml_load_string使用實(shí)例分享

先用一段代碼重現(xiàn)一下問(wèn)題

乍一看,結(jié)果很讓人費(fèi)解:

復(fù)制代碼 代碼如下:
<?php
$string = <<<EOF
<data>
<foo><bar>hello</bar></foo>
<foo><bar>world</bar></foo>
</data>
EOF;

$data = simplexml_load_string($string);

print_r($data);
print_r($data->foo);
?>

乍一看,結(jié)果很讓人費(fèi)解:

復(fù)制代碼 代碼如下:
SimpleXMLElement Object
(
[foo] => Array
(
[0] => SimpleXMLElement Object
(
[bar] => hello
)
[1] => SimpleXMLElement Object
(
[bar] => world
)
)
)
SimpleXMLElement Object
(
[bar] => hello
)

明明print_r顯示foo是一個(gè)有兩個(gè)bar元素的數(shù)組,但是最后卻僅僅顯示了一個(gè)bar元素!
原因其實(shí)很簡(jiǎn)單,在如上所示simplexml_load_string的結(jié)果里,foo并不是數(shù)組,而是一個(gè)迭代對(duì)象!
可以這樣確認(rèn):

復(fù)制代碼 代碼如下:
foreach ($data->foo as $v) print_r($v);
foreach ($data->children() as $v) print_r($v);

看來(lái),print_r或者var_dump之類的表象并不完全可信,自己多留心吧。

假如我們獲取的XML數(shù)據(jù)如下:(可以使用curl、fsockopen等方式獲取)

復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<dict num="219" id="219" name="219">
 <key>你好</key>
 <pos></pos>
 <acceptation>Array;Array;Array;</acceptation>
 <sent>
  <orig>Haven't seen you for a long time. How are you?</orig>
  <trans>多日不見(jiàn)了,你好嗎?</trans>
 </sent>
 <sent>
  <orig>Hello! How are you?</orig>
  <trans>嘿,你好?</trans>
 </sent>
 <sent>
  <orig>Hello, Brooks!How are you?</orig>
  <trans>喂,布魯克斯!你好嗎?</trans>
 </sent>
 <sent>
  <orig>Hi, Barbara, how are you?</orig>
  <trans>嘿,芭芭拉,你好嗎?</trans>
 </sent>
 <sent>
  <orig>How are you? -Quite well, thank you.</orig>
  <trans>你好嗎?-很好,謝謝你。</trans>
 </sent>
</dict>

經(jīng)過(guò)simplexml_load_string得到:

復(fù)制代碼 代碼如下:
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [num] => 219
            [id] => 219
            [name] => 219
        )

    [key] => 你好
    [pos] => SimpleXMLElement Object
        (
        )

    [acceptation] => Array;Array;Array;
    [sent] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [orig] => Haven't seen you for a long time. How are you?
                    [trans] => 多日不見(jiàn)了,你好嗎?
                )

            [1] => SimpleXMLElement Object
                (
                    [orig] => Hello! How are you?
                    [trans] => 嘿,你好?
                )

            [2] => SimpleXMLElement Object
                (
                    [orig] => Hello, Brooks!How are you?
                    [trans] => 喂,布魯克斯!你好嗎?
                )

            [3] => SimpleXMLElement Object
                (
                    [orig] => Hi, Barbara, how are you?
                    [trans] => 嘿,芭芭拉,你好嗎?
                )

            [4] => SimpleXMLElement Object
                (
                    [orig] => How are you? -Quite well, thank you.
                    [trans] => 你好嗎?-很好,謝謝你。
                )

        )

)

我們?cè)?a href=/itjie/phpjishu/ target=_blank class=infotextkey>php語(yǔ)言中可以用以下方法取得我們想要的值:

復(fù)制代碼 代碼如下:
<?php
$data = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<dict num="219" id="219" name="219">
 <key>你好</key>
 <pos></pos>
 <acceptation>Array;Array;Array;</acceptation>
 <sent>
  <orig>Haven't seen you for a long time. How are you?</orig>
  <trans>多日不見(jiàn)了,你好嗎?</trans>
 </sent>
 <sent>
  <orig>Hello! How are you?</orig>
  <trans>嘿,你好?</trans>
 </sent>
 <sent>
  <orig>Hello, Brooks!How are you?</orig>
  <trans>喂,布魯克斯!你好嗎?</trans>
 </sent>
 <sent>
  <orig>Hi, Barbara, how are you?</orig>
  <trans>嘿,芭芭拉,你好嗎?</trans>
 </sent>
 <sent>
  <orig>How are you? -Quite well, thank you.</orig>
  <trans>你好嗎?-很好,謝謝你。</trans>
 </sent>
</dict>
XML;
$xmldata = simplexml_load_string($data);
header("Content-Type: text/html; charset=UTF-8");
print_r($xmldata);
echo "<br />".trim($xmldata->sent[0]->orig); //Haven't seen you for a long time. How are you?
echo "<br />".trim($xmldata->key); //你好
?>

php技術(shù)php中simplexml_load_string使用實(shí)例分享,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 97精品一区二区 | 亚洲区一区二区 | 久久在看 | 亚洲国产一区视频 | 91精品麻豆日日躁夜夜躁 | gav成人免费播放视频 | 亚洲国产一区在线 | 一区二区三区国产精品 | 午夜丰满寂寞少妇精品 | 亚洲精品电影网在线观看 | 在线免费观看日本 | 黄色大片免费网站 | 五月香婷婷 | 日韩欧美国产一区二区 | 久久久久久国产精品免费免费狐狸 | 日韩有码一区 | 激情欧美日韩一区二区 | 国产小视频在线观看 | 热久色| 日韩精品久久一区二区三区 | 久久婷婷av | 成人激情免费视频 | 成人久久18免费网站图片 | 日韩视频在线观看中文字幕 | 久久精品视频9 | 精品91久久| 一区二区三区免费 | 男人天堂免费在线 | 欧美日韩专区 | 国产在线第一页 | 男人的天堂avav | 久久在看 | 色资源在线观看 | 欧美三级成人理伦 | 成人国产精品入口免费视频 | 欧美影院久久 | 国产一区二区三区在线 | 国内精品视频在线观看 | 久国产视频 | 一区二区日韩 | 久久网日本 |