检查它是否是来自 SimpleXMLElement 对象的数组

Ale*_*meo 1 php arrays object

我对以下 php 对象转储有问题。我尝试检查“价格”是否是一个数组,php 总是返回 false。数组“price”的var_dump()返回数组的第一个元素,而不是整个数组。哪个是错误的?如何检查它是否是一个数组?

object(SimpleXMLElement)#197 (1) {
    ["price"]=>  array(4) {
        [0]=>    object(SimpleXMLElement)#156 (4) {
            ["room_id"]=>      string(4) "1755"
            ["room_seq"]=>      string(1) "1"
            ["offer_id"]=>      string(4) "5842"
            ["price_total"]=>      object(SimpleXMLElement)#205 (2) {
                ["price_typ"]=>        string(1) "0"
                ["price_hb"]=>        string(4) "2450"
            }
        }
        [1]=>    object(SimpleXMLElement)#143 (4) {
            ["room_id"]=>      string(5) "24206"
            ["room_seq"]=>      string(1) "1"
            ["offer_id"]=>      string(4) "5842"
            ["price_total"]=>      object(SimpleXMLElement)#205 (2) {
                ["price_typ"]=>        string(1) "0"
                ["price_hb"]=>        string(4) "2450"
            }
        }
        [2]=>    object(SimpleXMLElement)#135 (4) {
            ["room_id"]=>      string(4) "1755"
            ["room_seq"]=>      string(1) "1"
            ["offer_id"]=>      string(6) "415725"
            ["price_total"]=>      object(SimpleXMLElement)#205 (2) {
                ["price_typ"]=>        string(1) "0"
                ["price_hb"]=>        string(6) "2327.5"
            }
        }
        [3]=>    object(SimpleXMLElement)#136 (4) {
            ["room_id"]=>      string(5) "24206"
            ["room_seq"]=>      string(1) "1"
            ["offer_id"]=>      string(6) "415725"
            ["price_total"]=>      object(SimpleXMLElement)#205 (2) {
                ["price_typ"]=>        string(1) "0"
                ["price_hb"]=>        string(6) "2327.5"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*ler 5

考虑一个简化的例子:

<root>
    <element>first</element>
    <element>second</element>
    <element>third</element>
</root>
Run Code Online (Sandbox Code Playgroud)

$root->element并不是真正的数组。它是SimpleXMLElement对象。您可以将其视为选择器,您可以使用它来遍历元素的集合foreach,还可以使用索引来访问特定对象:

$root->element[0]; //first object
$root->element[1]; //second
$root->element[2]; //third
Run Code Online (Sandbox Code Playgroud)

以下是基本用法的更多示例:http ://php.net/manual/en/simplexml.examples-basic.php


所以真正的问题是:如何定义集合中是否有多个元素?

您可以使用count()方法来做到这一点:

if($es->result->hotel->channel->room_price->price->count() > 1){
    echo 'many elements';
}
Run Code Online (Sandbox Code Playgroud)

阅读更多: http: //php.net/manual/en/simplexmlelement.count.php