simplexml 路径(按索引)

发布于 2024-09-14 23:21:39 字数 1870 浏览 2 评论 0原文

我正在解析 simplexml,通常我的简单 xml 看起来像这样,

        $sig_size = (int)$xmlObject->sig->points;

在这个例子中返回 24

 <?xml version='1.0' standalone='yes'?>
 <photo id='470'>
    <artist>mg</artist>
    <lines>
        <points>22</points>
        <angle>-5</angle>
        <x>165</x>
        <align>center</align>
        <color>ffffff</color>
    </lines>
    <sig>
        <padding>35</padding>
        <x>175</x>
        <y>300</y>
        <points>24</points>
        <angle>-5</angle>
        <align>center</align>
        <color>ffffff</color>
    </sig>
 </photo>

现在我想添加第二个 sig 项并通过索引引用它,所以 xml 看起来像这样

 <?xml version='1.0' standalone='yes'?>
 <photo id='470'>
    <artist>mg</artist>
    <lines>
        <points>22</points>
        <angle>-5</angle>
        <x>165</x>
        <align>center</align>
        <color>ffffff</color>
    </lines>
    <sig>
        <padding>35</padding>
        <x>175</x>
        <y>300</y>
        <points>24</points>
        <angle>-5</angle>
        <align>center</align>
        <color>ffffff</color>
    </sig>
    <sig>
        <padding>35</padding>
        <x>175</x>
        <y>300</y>
        <points>10</points>
        <angle>-5</angle>
        <align>center</align>
        <color>ffffff</color>
    </sig>
 </photo>

,那么我如何重写php行通过索引获取它

I'm parsing simplexml and normally my simple xml would look like this

        $sig_size = (int)$xmlObject->sig->points;

which returns 24 in this example

 <?xml version='1.0' standalone='yes'?>
 <photo id='470'>
    <artist>mg</artist>
    <lines>
        <points>22</points>
        <angle>-5</angle>
        <x>165</x>
        <align>center</align>
        <color>ffffff</color>
    </lines>
    <sig>
        <padding>35</padding>
        <x>175</x>
        <y>300</y>
        <points>24</points>
        <angle>-5</angle>
        <align>center</align>
        <color>ffffff</color>
    </sig>
 </photo>

now I want to add a second sig item and refeence it by the index so the xml would look like this

 <?xml version='1.0' standalone='yes'?>
 <photo id='470'>
    <artist>mg</artist>
    <lines>
        <points>22</points>
        <angle>-5</angle>
        <x>165</x>
        <align>center</align>
        <color>ffffff</color>
    </lines>
    <sig>
        <padding>35</padding>
        <x>175</x>
        <y>300</y>
        <points>24</points>
        <angle>-5</angle>
        <align>center</align>
        <color>ffffff</color>
    </sig>
    <sig>
        <padding>35</padding>
        <x>175</x>
        <y>300</y>
        <points>10</points>
        <angle>-5</angle>
        <align>center</align>
        <color>ffffff</color>
    </sig>
 </photo>

so how do I re-write the php line to get it by index

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

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

发布评论

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

评论(1

财迷小姐 2024-09-21 23:21:39

您必须使用括号 []:

$sig_size_one = (int)$xmlObject->sig[0]->points;
$sig_size_two = (int)$xmlObject->sig[1]->points;

You would have to use brackets []:

$sig_size_one = (int)$xmlObject->sig[0]->points;
$sig_size_two = (int)$xmlObject->sig[1]->points;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文