如何获取simplexmlobject php的位置
我有以下简单的xml对象文件:如何获取simplexmlobject php的位置,php,multidimensional-array,simplexml,Php,Multidimensional Array,Simplexml,我有以下简单的xml对象文件: [AuthorList] => SimpleXMLElement Object ( [@attributes] => Array ( [CompleteYN] => Y ) [Author] => Array ( [0] => SimpleXMLElement Object (
[AuthorList] => SimpleXMLElement Object
(
[@attributes] => Array
(
[CompleteYN] => Y
)
[Author] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ValidYN] => Y
)
[LastName] => van Tricht
[ForeName] => M J
[Initials] => MJ
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ValidYN] => Y
)
[LastName] => Nieman
[ForeName] => D H
[Initials] => DH
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ValidYN] => Y
)
[LastName] => Bour
[ForeName] => L J
[Initials] => LJ
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ValidYN] => Y
)
[LastName] => Boerée
[ForeName] => T
[Initials] => T
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ValidYN] => Y
)
[LastName] => Koelman
[ForeName] => J H T M
[Initials] => JH
)
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ValidYN] => Y
)
[LastName] => de Haan
[ForeName] => L
[Initials] => L
)
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[ValidYN] => Y
)
[LastName] => Linszen
[ForeName] => D H
[Initials] => DH
)
)
)
例如,现在我想输出一个名字的位置
职位姓氏Boree为2 0f 6
(0是起始索引)
有人知道吗?使用for循环。
假设$authorList是simpleXML对象
$authorsCount = count($authorList->author);
$result=-1;
for($i=0;$i<$authorsCount;$i++){
if($authorList->author[$i]->LastName =="Boeree"){
$result=$i;
break;
}
if($result==-1) echo "Boereee not found";
else echo "position LastName Boree is {$result} of {$authorsCount}";
$authorsCount=count($authorList->author);
$result=-1;
对于($i=0;$iauthor[$i]->LastName==“Boree”){
$result=$i;
打破
}
如果($result==-1)回显“未找到Boreee”;
else echo“position LastName Boree是{$authorsCount}的{$result}”;
编辑:编辑代码以使用simpleXML对象而不是数组使用for循环。
假设$authorList是simpleXML对象
$authorsCount = count($authorList->author);
$result=-1;
for($i=0;$i<$authorsCount;$i++){
if($authorList->author[$i]->LastName =="Boeree"){
$result=$i;
break;
}
if($result==-1) echo "Boereee not found";
else echo "position LastName Boree is {$result} of {$authorsCount}";
$authorsCount=count($authorList->author);
$result=-1;
对于($i=0;$iauthor[$i]->LastName==“Boree”){
$result=$i;
打破
}
如果($result==-1)回显“未找到Boreee”;
else echo“position LastName Boree是{$authorsCount}的{$result}”;
编辑:编辑代码以使用simpleXML对象而不是数组Hi thank。。。但是,我对php和数组功能非常陌生,你能告诉我如何从上面的数组中获取有效变量中的作者吗?如果你的对象名是$authorList,它应该使用$authors=$authorList->author;嗨,谢谢。。。但是,我对php和数组功能非常陌生,你能告诉我如何从上面的数组中获取有效变量中的作者吗?如果你的对象名是$authorList,它应该使用$authors=$authorList->author;