如何获取simplexmlobject php的位置

发布于 2024-10-07 21:39:44 字数 2798 浏览 6 评论 0原文

我有以下简单的 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 是起始索引)

有谁知道这一点吗?

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
                )

        )
)

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贵在坚持 2024-10-14 21:39:44

使用 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 对象而不是数组

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文