使用SimpleXMLElement从对象获取数组

时间:2021-07-15 16:52:10

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.

我在这些对象中获取数组时遇到了一些问题。当我print_r()时,将打印以下代码。 $ message_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从对象中获取id数组:

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?

我如何获得[0]的值或者我是否会出错?有没有办法循环结果并获得对象键?

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

我试图回显$ id [0]但它没有返回任何结果。

3 个解决方案

#1


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.

当你在SimpleXMLElement上使用print_r时,两者之间会产生魔力。所以你看到的实际上并不是什么。它提供了丰富的信息,但与普通对象或数组不同。

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.:

因为这仍然会给你SimpleXMLElements但是你可能希望有值,你可以在使用时将每个元素转换为字符串,例如:

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));

#2


1  

You can cast the SimpleXMLElement object like so:

您可以像这样强制转换SimpleXMLElement对象:

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:

好的,上面的array_map代码并没有真正起作用,因为它不是严格意义上的数组,你应该先应用iterator_to_array($ message_object-> data_id,false):

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

See also: @hakre's answer.

另见:@ hakre的答案。

#3


0  

You just need to update your foreach like this:

你只需要像这样更新你的foreach:

foreach($message_object->data->id as $key => $value) {
    print_r($value);
}

#1


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.

当你在SimpleXMLElement上使用print_r时,两者之间会产生魔力。所以你看到的实际上并不是什么。它提供了丰富的信息,但与普通对象或数组不同。

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.:

因为这仍然会给你SimpleXMLElements但是你可能希望有值,你可以在使用时将每个元素转换为字符串,例如:

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));

#2


1  

You can cast the SimpleXMLElement object like so:

您可以像这样强制转换SimpleXMLElement对象:

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:

好的,上面的array_map代码并没有真正起作用,因为它不是严格意义上的数组,你应该先应用iterator_to_array($ message_object-> data_id,false):

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

See also: @hakre's answer.

另见:@ hakre的答案。

#3


0  

You just need to update your foreach like this:

你只需要像这样更新你的foreach:

foreach($message_object->data->id as $key => $value) {
    print_r($value);
}