Getting an array from object using SimpleXMLElemen

2019-05-11 17:13发布

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.

3条回答
叛逆
2楼-- · 2019-05-11 18:00

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));
查看更多
叼着烟拽天下
3楼-- · 2019-05-11 18:09

You just need to update your foreach like this:

foreach($message_object->data->id as $key => $value) {
    print_r($value);
}
查看更多
倾城 Initia
4楼-- · 2019-05-11 18:10

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.

查看更多
登录 后发表回答