来自xml_parse_into_struct()的bizzare数组结构 [英] bizzare array structure from xml_parse_into_struct()

查看:96
本文介绍了来自xml_parse_into_struct()的bizzare数组结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试xml_parse_into_struct(),所以我举了一下这个例子。
www.php .net 并将此代码放在网站上:


<?php

$ simple =<<< END

< item>

< cbId> 345< / cbId>

< cbHeadline>嘿那里< / cbHeadline>

< cbMainContent>上帝并没有死,他只是睡觉了。< / cbMainContent>

< / item>

< item>

< cbId> 432< / cbId>

< cbHeadline>狗是什么狗,狗是什么< / cbHeadline>

< cbMainContent>在经理中。< / cbMainContent>

< / item>

END;


$ p = xml_parser_create();

xml_parse_into_struct($ p,$ simple,$ vals,$ index);

xml_parser_free($ p);

echo" index array\\\
";

print_r($ index);

echo" \ nVals arra y\\\
英寸;

的print_r($瓦尔斯);

>


不过,我回来做对我没有意义。只有第一个ITEM才将它们放入数组中,所有其他标签都被计算为

ITEMS。为什么?


如果我只想将我的XML代码变成二维数组,那么

是最简单的方法吗? xml_parse_into_struct是否正常工作

,我只是误解了它,或者还有另外一种方式让我这么做吗?


索引数组

数组



[ITEM] =>数组



[0] => 0

[1] => 2

[2] => 3

[3] =大于5

[4] =→6

[5] => 8

[6] => 9 < br $> b $ b)


[CBID] =>数组



[0] => 1




[CBHEADLINE] =>数组



[0] = →4

)中


[CBMAINCONTENT] =>阵列



[0 ] => 7







Vals数组

数组



[0] =>数组



[tag] => ITEM

[type] => open

[level] => 1

[value] =>





[1] =>数组



[标签] => CBID

[type] =>完成

[等级] => 2

[value] => 345




[2] =>数组



[tag] =>项目

[value] =>


[type] => cdata

[等级] => 1




[3] =>数组



[tag] =>项目

[value] =>

[类型] => cdata

[等级] => 1




[4] =>数组



[tag] => CBHEADLINE

[type] =>完成

[等级] => 2

[value] =>嘿那里




[5] =>数组



[tag] =>项目

[value] =>


[型] => CDATA

[等级] =→1个

)中


[6] =>数组



[tag] =>项目

[value] =>

[类型] => cdata

[等级] => 1




[7] =>数组



[tag] => CBMAINCONTENT

[type] =>完成

[等级] => 2

[value] =>上帝没有死,他只是在睡觉。




[8] =>数组



[tag] =>项目

[value] =>


[type] => cdata

[等级] => 1




[9] =>数组



[tag] =>项目

[类型] =>关闭

[等级] => 1





I wanted to test xml_parse_into_struct() so I took the example off of
www.php.net and put this code up on a site:

<?php
$simple = <<<END
<item>
<cbId>345</cbId>
<cbHeadline>Hey there</cbHeadline>
<cbMainContent>God is not dead, he is merely
sleeping.</cbMainContent>
</item>
<item>
<cbId>432</cbId>
<cbHeadline>What a dog, what a dog</cbHeadline>
<cbMainContent>In the manger.</cbMainContent>
</item>
END;

$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
?>

However, what I got back made no sense to me. Only the first ITEM made
it into the arrays, and all the other tags somehow got counted as
ITEMS. Why?

If I simply want my XML code turned into a two dimensional array, what
is the simplest way to do it? Is xml_parse_into_struct working
correctly, and I simply misunderstand it, or is there another way for
me to do this?

Index array
Array
(
[ITEM] => Array
(
[0] => 0
[1] => 2
[2] => 3
[3] => 5
[4] => 6
[5] => 8
[6] => 9
)

[CBID] => Array
(
[0] => 1
)

[CBHEADLINE] => Array
(
[0] => 4
)

[CBMAINCONTENT] => Array
(
[0] => 7
)

)

Vals array
Array
(
[0] => Array
(
[tag] => ITEM
[type] => open
[level] => 1
[value] =>

)

[1] => Array
(
[tag] => CBID
[type] => complete
[level] => 2
[value] => 345
)

[2] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 1
)

[3] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)

[4] => Array
(
[tag] => CBHEADLINE
[type] => complete
[level] => 2
[value] => Hey there
)

[5] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 1
)

[6] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)

[7] => Array
(
[tag] => CBMAINCONTENT
[type] => complete
[level] => 2
[value] => God is not dead, he is merely sleeping.
)

[8] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 1
)

[9] => Array
(
[tag] => ITEM
[type] => close
[level] => 1
)

)

推荐答案

simple = <<<< END

< item>

< cbId> 345< / cbId>

< cbHeadline>嘿那里< ; / cbHeadline>

< cbMainContent>上帝没有死,他只是睡觉。< / cbMainContent>

< / item>

< item>

< cbId> 432< / cbId>

< cbHeadline>狗是什么狗,狗是什么< ; / cbHeadline>

< cbMainContent>在经理中。< / cbMainContent>

< / item>

END;

simple = <<<END
<item>
<cbId>345</cbId>
<cbHeadline>Hey there</cbHeadline>
<cbMainContent>God is not dead, he is merely
sleeping.</cbMainContent>
</item>
<item>
<cbId>432</cbId>
<cbHeadline>What a dog, what a dog</cbHeadline>
<cbMainContent>In the manger.</cbMainContent>
</item>
END;


p = xml_parser_create();

xml_parse_into_struct(
p = xml_parser_create();
xml_parse_into_struct(


p,


这篇关于来自xml_parse_into_struct()的bizzare数组结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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