PHP:从SimpleXML的阵列特定值 [英] PHP: Get specific value from simplexml array

查看:105
本文介绍了PHP:从SimpleXML的阵列特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新的PHP的XML,希望你能帮助我与此有关。
搜索论坛没帮我还没有找到一个答案,我的具体问题。

I am pretty new to PHP an XML and hope you can help me with this. Searching the forum didn't help me yet to find an answer to my specific issue.

我有一个数组的SimpleXML看起来像下面的PHP页面,只是长:

I have a PHP page with a simplexml array that looks like the following, just longer:

    SimpleXMLElement Object 
    ( 
       [textID] => Array 
                ( 
                   [0] => SimpleXMLElement Object 
                       ( 
                          [textID] => 1 
                          [content] => Text1 
                       ) 
                   [1] => SimpleXMLElement Object 
                       ( 
                          [textID] => 2 
                          [content] => Text2
                       ) 
                   [2] => SimpleXMLElement Object 
                       ( 
                          [textID] => 3 
                          [content] => Text3 
                       ) 
                )
      )

现在我想通过参考其ID是一个整数呼应了此数组特定的值。
我得到这个工作的唯一方法是以下但这只是顺序进入阵中,而不是实际的ID:

Now I am trying to echo out a specific value from this array by referring to its ID which is an integer. The only way I get this working is the following but this just goes by the order within the array, not by the actual ID:

    <?php echo $objTexts->textID[1]->content; ?>

谁能告诉我什么,我在这里丢失?

Can someone tell me what I am missing here ?

谢谢,蒂姆

推荐答案

的SimpleXML没有办法知道的 TEXTID 确定哪个节点是 - 它只是另一个元素在XML。

SimpleXML has no way of knowing that the textID identifies which node is which - it is just another element in the XML.

根据您的采样输出,你的XML是有点混乱,你都要求多个元素 TEXTID 它们都有一个孩子,也称为 TEXTID ,它有不同的含义。然而,你想做的事,可以通过所有的外 TEXTID 元素循环和测试他们的内在价值或者取得什么 TEXTID 元素:

Based on your sample output, your XML is a little confusing as you have multiple elements called textID which each have a single child, also called textID, which has a different meaning. Nonetheless, what you want to do can be achieved either by looping through all the outer textID elements and testing the value of their inner textID element:

foreach ( $objTexts->textID as $item )
{
     if ( $item->textID == '2' )
     {
          ...
     }
}

或者,也可以使用XPath,它是对XML一个相当简单的查询语言,并在 - &GT;的XPath()方法。在你的情况,你想要找到其中包含与特定值 TEXTID 孩子 TEXTID 节点,因此code语言会看起来是这样的:

Or, you could use XPath, which is a fairly simple query language for XML, and is supported within SimpleXML in the form of the ->xpath() method. In your case, you want to find a textID node which contains a textID child with a particular value, so the code would look something like this:

 // ->xpath always returns a plain PHP array - not a SimpleXML object
 $xpath_results = $objTexts->xpath('//textID[textID=2]');

 // If you're certain you only want the first result:
 echo $xpath_results[0]->content;

 // If you might want multiple matches
 foreach ( $xpath_results as $item )
 {
     ...
 }

这篇关于PHP:从SimpleXML的阵列特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆