我有一个simplexml脚本创建一个复杂的对象,我只想从div'网格'中获取信息,所以抓住我使用xpath来获取它。
$id = $sxml->xpath("//*[@id='grid_data']");
这导致大对象数组似乎是对象和数组的混合,我真的很难通过它遍历我的方式。以下是一个非常简化的版本。有30个成员'人1'等。每个人都有一个包含25个项目的列表,我需要访问/工作这些项目。 ([“li”] =>数组(25))
理想情况下,我需要循环遍历每个成员,然后循环遍历每个li项目,但是我会挂断使用$ variable ['name'] vs $ object-> name
只是测试我已经尝试了各种方法来获取人名,我想我很困惑自己试图绕过物体遍历。
echo $id[0]->div[0][p][a];
echo $id[0]['div'][0]['p']['a'];
echo $id->0->div
array(1) {
[0]=>
object(SimpleXMLElement)#3 (2) {
["@attributes"]=>
array(1) {
["id"]=>
string(9) "grid_data"
}
["div"]=>
array(35) {
[0]=>
object(SimpleXMLElement)#4 (4) {
["@attributes"]=>
array(1) {
["class"]=>
string(9) "stff_grid"
}
["p"]=>
object(SimpleXMLElement)#39 (2) {
["@attributes"]=>
array(2) {
["class"]=>
string(16) "staff"
["id"]=>
string(15) "1328"
}
["a"]=>
string(17) "Person 1"
}
["ul"]=>
object(SimpleXMLElement)#40 (2) {
["@attributes"]=>
array(1) {
["class"]=>
string(0) ""
}
["li"]=>
array(25) {
[0]=>
object(SimpleXMLElement)#42 (2) {
["@attributes"]=>
array(1) {
["class"]=>
string(16) "lrge"
}
["a"]=>
string(2) "00"
}
[1]=>
object(SimpleXMLElement)#43 (2) {
["@attributes"]=>
array(1) {
["class"]=>
string(16) "lrge"
}
["a"]=>
string(2) "01"
}
[2]=>
object(SimpleXMLElement)#44 (2) {
["@attributes"]=>
array(1) {
["class"]=>
string(16) "lrge"
}
["a"]=>
string(2) "02"
}
}
}
["div"]=>
object(SimpleXMLElement)#41 (1) {
["@attributes"]=>
array(1) {
["class"]=>
string(10) "left"
}
}
}
[1]=>
object(SimpleXMLElement)#5 (4) {
["@attributes"]=>
array(1) {
["class"]=>
string(9) "stff_grid"
}
["p"]=>
object(SimpleXMLElement)#41 (2) {
["@attributes"]=>
array(2) {
["class"]=>
string(16) "staff"
["id"]=>
string(15) "no_1333"
}
["a"]=>
string(11) "Person 2"
}
["ul"]=>
object(SimpleXMLElement)#40 (2) {
["@attributes"]=>
array(1) {
["class"]=>
string(0) ""
}
["li"]=>
array(25) {
[0]=>
object(SimpleXMLElement)#66 (2) {
["@attributes"]=>
array(1) {
["class"]=>
string(16) "lrge"
}
["a"]=>
string(2) "00"
}
[1]=>
object(SimpleXMLElement)#65 (2) {
["@attributes"]=>
array(1) {
["class"]=>
string(16) "lrge"
}
["a"]=>
string(2) "01"
}
[2]=>
object(SimpleXMLElement)#64 (2) {
["@attributes"]=>
array(1) {
["class"]=>
string(16) "lrge"
}
["a"]=>
string(2) "02"
}
}
}
["div"]=>
object(SimpleXMLElement)#39 (1) {
["@attributes"]=>
array(1) {
["class"]=>
string(10) "left"
}
}
}
[2]=>
object(SimpleXMLElement)#6 (1) {
["@attributes"]=>
array(1) {
["class"]=>
string(6) "spacer"
}
}
答案 0 :(得分:1)
让simplexml
和xpath
一起使用,请参阅下面的评论
<?php
$xml = simplexml_load_file('xml.xml');
// let's take all the divs that have the class "stff_grid"
$divs = $xml->xpath("//*[@class='stff_grid']");
// for each of these elements, let's print out the value inside the first p tag
foreach($divs as $div){
print $div->p->a . PHP_EOL;
// now for each li tag let's print out the contents inside the a tag
foreach ($div->ul->li as $row){
print " - " . $row->a . PHP_EOL;
}
}
/* this outputs the following
Person 1
- 1 hr
- 2 hr
- 3 hr
- 4 hr
- 5 hr
- 6 hr
- 7 hr
- 8 hr
Person 2
- 1 hr
- 2 hr
- 3 hr
- 4 hr
- 5 hr
- 6 hr
- 7 hr
- 8 hr
Person 3
- 1 hr
- 2 hr
- 3 hr
- 4 hr
- 5 hr
- 6 hr
- 7 hr
- 8 hr
*/