开发者

how to get position of simplexmlobject php

I have the following simple xml object file:

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

        )
)
开发者_Python百科

Now i want to output the position of one name, for example

position LastName Boeree is 2 0f 6

(0 is the starting index)

Does anyone know this?


Use a for loop. Lets say $authorList is your simpleXML object

$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}";

EDIT: edited code to use the simpleXML object instead of an array

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜