3

I'm having some problems getting the array in these objects. When I print_r(), the following code is printed. $message_object is the name of the object.

SimpleXMLElement Object
(
    [header] => SimpleXMLElement Object
        (
            [responsetime] => 2012-12-22T14:10:09+00:00
        )

    [data] => SimpleXMLElement Object
        (
            [id] => Array
                (
                    [0] => 65233
                    [1] => 65234
                )

            [account] => Array
                (
                    [0] => 20992
                    [1] => 20992
                )

            [shortcode] => Array
                (
                    [0] => 3255
                    [1] => 3255
                )

            [received] => Array
                (
                    [0] => 2012-12-22T11:04:30+00:00
                    [1] => 2012-12-22T11:31:08+00:00
                )

            [from] => Array
                (
                    [0] => 6121843347
                    [1] => 6121820166
                )

            [cnt] => Array
                (
                    [0] => 24
                    [1] => 25
                )

            [message] => Array
                (
                    [0] => Go tramping wellington 11-30
                    [1] => Go drinking Matakana 2pm
                )

        )

)

I'm trying to get the id arrays out of the objects with a foreach:

foreach($message_object->data->id AS $id) {
    print_r($id);
}

The following reply is sent:

SimpleXMLElement Object ( [0] => 65233 ) SimpleXMLElement Object ( [0] => 65234 )

How do I get the value of [0] or am I going about this wrong? and is there a way to loop though the results and get the object keys?

I have tried to echo $id[0] but it returns no result.

hakre
  • 193,403
  • 52
  • 435
  • 836
Philip
  • 393
  • 3
  • 9
  • try looping this first: foreach($message_object AS $k=>$val) then you should be able to add an if statement to check for the $k = 0. – MrTechie Dec 22 '12 at 02:22
  • did you try echoing just $id instead of $id[0]? – kennypu Dec 22 '12 at 02:25
  • When I print_r($id) it gives me SimpleXMLElement Object ( [0] => 65233 ) so by that theroy $id[0] should = 65233. – Philip Dec 22 '12 at 02:31

3 Answers3

4

When you use print_r on a SimpleXMLElement there comes magic in between. So what you see is not actually what is there. It's informative, but just not the same as with normal objects or arrays.

To answer your question how to iterate:

foreach ($message_object->data->id as $id)
{
    echo $id, "\n";
}

to answer how to convert those into an array:

$ids = iterator_to_array($message_object->data->id, 0);

As this would still give you the SimpleXMLElements but you might want to have the values you can either cast each of these elements to string on use, e.g.:

echo (string) $ids[1]; # output second id 65234

or convert the whole array into strings:

$ids = array_map('strval', iterator_to_array($message_object->data->id, 0));

or alternatively into integers:

$ids = array_map('intval', iterator_to_array($message_object->data->id, 0));
hakre
  • 193,403
  • 52
  • 435
  • 836
1

You can cast the SimpleXMLElement object like so:

foreach ($message_object->data->id AS $id) {
    echo (string)$id, PHP_EOL;
    echo (int)$id, PHP_EOL; // should work too

    // hakre told me that this will work too ;-)
    echo $id, PHP_EOL;
}

Or cast the whole thing:

$ids = array_map('intval', $message_object->data->id);
print_r($ids);

Update

Okay, the array_map code just above doesn't really work because it's not strictly an array, you should apply iterator_to_array($message_object->data_id, false) first:

$ids = array_map('intval', iterator_to_array$message_object->data->id, false));

See also: @hakre's answer.

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • `$message_object->data->id` is not an array, so `array_map` will complain about that. Use `iterator_to_array` with no keys here first, see my answer http://stackoverflow.com/a/13999675/367456 – hakre Dec 22 '12 at 03:00
0

You just need to update your foreach like this:

foreach($message_object->data->id as $key => $value) {
    print_r($value);
}
PhearOfRayne
  • 4,990
  • 3
  • 31
  • 44
  • $value then = SimpleXMLElement Object ( [0] => 65233 ) SimpleXMLElement Object ( [0] => 65234 ) I'm trying to get the value [0] from the object echo $value[0]; returns null – Philip Dec 22 '12 at 02:55
  • @Philip: Yes, because that `[0]` is virtual. `print_r` is fooling you a bit here because this magic is special with `SimpleXMLElement`. See my answer http://stackoverflow.com/a/13999675/367456 – hakre Dec 22 '12 at 02:56
  • @hakre you have extended my knowledge about SimpleXMLElement. Thank you. – Philip Dec 23 '12 at 03:23