drxdn40242 2017-05-02 14:47
浏览 53
已采纳

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

I have a problem with the follow php object dump. I try to check if "price" is an array, php return me always false. var_dump() of array "price" return me first element of array and not the entire array. Which is wrong? How can I check if it is an array?

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"
            }
        }
    }
}
  • 写回答

2条回答 默认 最新

  • douhuocuo9012 2017-05-02 20:15
    关注

    Consider a simplified example:

    <root>
        <element>first</element>
        <element>second</element>
        <element>third</element>
    </root>
    

    $root->element is not really an array. It is SimpleXMLElement object. You can think of it as a selector, a collection of elements you can traverse using foreach and also access specific object using indexes:

    $root->element[0]; //first object
    $root->element[1]; //second
    $root->element[2]; //third
    

    Here is more examples of basic usage: http://php.net/manual/en/simplexml.examples-basic.php


    So the real question is: How to define if there is more than one element in collection ?

    You can do it using count() method:

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

    Read more: http://php.net/manual/en/simplexmlelement.count.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?