如何获取simplexml对象的位置php

时间:2010-12-15 12:14:57

标签: php multidimensional-array simplexml

我有以下简单的xml对象文件:

[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
                )

        )
)

现在我想输出一个名字的位置,例如

位置LastName Boeree是2 0f 6

(0是起始索引)

有谁知道这个?

1 个答案:

答案 0 :(得分:1)

使用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}";

编辑:编辑代码以使用simpleXML对象而不是数组