彼此内部的 simplexml 对象和数组
simplexml objects and arrays within each other
我有一个创建复杂对象的 simplexml 脚本,我只想要 div 'grid' 中的信息,所以抓取我使用 xpath 来获取它。
$id = $sxml->xpath("//*[@id='grid_data']");
这导致了大对象数组,它似乎是对象和数组的混合体,我真的很难遍历它。下面是一个非常精简的版本。有 30 个成员 'Person 1' 等。每个人都有一个包含 25 个项目的列表,我需要对这些项目进行 access/work。 (["li"]=>数组(25))
理想情况下,我需要循环遍历每个成员,然后循环遍历每个 li 项,但我对使用 $variable['name'] 与 $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"
}
}
让我们将 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
*/
我有一个创建复杂对象的 simplexml 脚本,我只想要 div 'grid' 中的信息,所以抓取我使用 xpath 来获取它。
$id = $sxml->xpath("//*[@id='grid_data']");
这导致了大对象数组,它似乎是对象和数组的混合体,我真的很难遍历它。下面是一个非常精简的版本。有 30 个成员 'Person 1' 等。每个人都有一个包含 25 个项目的列表,我需要对这些项目进行 access/work。 (["li"]=>数组(25))
理想情况下,我需要循环遍历每个成员,然后循环遍历每个 li 项,但我对使用 $variable['name'] 与 $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"
}
}
让我们将 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
*/