php解析xml到二维数组问题,求大神指导
<br /><?xml version="1.0" encoding="gb2312"?><br /><www><br /><hh name="haha1哈哈" /><br /><hh name="haha2哈哈"><br /><hh name="haha2.1哈哈" /><br /><hh name="haha2.2哈哈" /></hh><br /><hh name="haha3哈哈"><br /><hh name="haha3.1哈哈" /><br /><hh name="haha3.2哈哈" /><br /></hh><br /></www><br />
<br /><?php<br />$xml = simplexml_load_file("hh.xml");<br />$arr = json_decode(json_encode($xml),TRUE);<br />echo '<meta http-equiv="content-type" content="text/html;charset=utf-8">';<br />print_r(arrto2($arr['hh']));<br />function arrto2($arr,$pid=0){<br /> static $i=1;<br /> static $data=array();<br /> foreach($arr as $v){<br /> if(is_array($v)){<br /> foreach($v as $z => $x){<br /> if($z==="@attributes"){<br /> $v["@attributes"]['id']=$i;<br /> $v["@attributes"]['pid']=$pid;<br /> $data[$i]=$v["@attributes"];<br /><br /> }else{<br /> arrto2($x,$i);<br /> }<br /> <br /> $i++;<br /> }<br /> }<br /> }<br /> return $data;<br />}<br />?><br />
<br>Array<br>(<br> [1] => Array<br> (<br> [name] => haha1哈哈<br> [id] => 1<br> [pid] => 0<div class="clear"> </div>