simpleXML object里面不能读出数据怎么办?

WBOY
Release: 2016-06-23 14:01:47
Original
930 people have browsed it

本帖最后由 joebnb 于 2014-02-26 11:01:22 编辑

class just{	function xmlInclude($data){		$xml = simplexml_load_file(dirname(__FILE__).'/jump.xml');		foreach($xml->Keyword->xpath("//*[data='$data']") as $item){		  return $item;		}	}}$TR = new just;$cr = $TR->xmlInclude(查询);print_r($cr);$cr=(array)$cr;echo '<br>';print_r($cr);
Copy after login

写了一个simplexml解析,print_r发现它出来的是simpleXML object不是数组
SimpleXMLElement Object ( [data] => 查询 [page] => Array ( [0] => dbcon.php [1] => select.php [2] => footer.php ) ) Array ( [data] => 查询 [page] => Array ( [0] => dbcon.php [1] => select.php [2] => footer.php ) )
Copy after login

里面的数值怎么提取,百度上的答案都是转换为数组,我也试着这么做了
可是出来后
echo $cr[0].$cr[0][0].$cr[1][0]
Copy after login

都不能获取到数值并提示:
Notice: Undefined offset: 0 in D:\Program files\WEB Edit\phpLight2013\WWW\Core\xml.php on line 23
Notice: Undefined offset: 0 in D:\Program files\WEB Edit\phpLight2013\WWW\Core\xml.php on line 23

请问怎么样才能从simplexml obj里直接取值?像这种数组中还有数组的情况如何取值?


回复讨论(解决方案)

$cr = $TR->xmlInclude(查询);echo $cr->data;echo $cr->page[1];
Copy after login
Copy after login

$cr = $TR->xmlInclude(查询);echo $cr->data;echo $cr->page[1];
Copy after login
Copy after login

谢谢你!我还想知道如果是数组怎么办?我是新手,所以遇到的东西都想弄明白

$cr = $TR->xmlInclude(查询);
$cr=(array)$cr;
echo $cr['data'];
echo $cr['page'][1];

注意:$cr=(array)$cr; 只是将第一层对象转换成了数组

谢谢版主!~~

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template