Alessandro Romeo
Alessandro Romeo

Reputation: 108

Check if it is an array from SimpleXMLElement object

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"
            }
        }
    }
}

Upvotes: 1

Views: 3295

Answers (2)

Lo Vega
Lo Vega

Reputation: 121

// simplexml_load_string() return false if not $XML
$xml = simplexml_load_string($pSdata);

if(gettype($xml)=='object')
{
    print_r(simplexmlArray($xml));
} else {
    echo "no xml input";
}

// simplexml_load $xml obj to array
function simplexmlArray ( $xmlObject, $out = array () )
{
    foreach ( (array) $xmlObject as $index => $node )
        $out[$index] = ( is_object ( $node ) ) ? simplexmlArray ( $node ) : $node;
    return $out;
}

Upvotes: -1

Alexey Chuhrov
Alexey Chuhrov

Reputation: 1787

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

Upvotes: 5

Related Questions