2012-12-22 236 views
3

我在获取这些对象中的数组时遇到了一些问题。当我print_r()时,打印下面的代码。 $ message_object是对象的名称。使用SimpleXMLElement从对象获取数组

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 
       ) 

     ) 

) 

我试图让ID阵列出在foreach对象:

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

下发送回复:

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

我如何获得价值[0]还是我正在讨论这个错误?有没有办法循环通过结果并获得对象键?

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

+0

尝试循环这个第一:的foreach($ message_object为K => $ VAL $),那么你应该能够添加一个if语句来检查$ k = 0。 – MrTechie

+0

您是否尝试回显$ id而不是$ id [0]? – kennypu

+0

当我print_r($ id)它给了我SimpleXMLElement对象([0] => 65233),因此theroy $ id [0]应该= 65233. – Philip

回答

4

当您在SimpleXMLElement上使用print_r时,会出现魔术。所以你看到的并不是实际存在的东西。它的信息丰富,但与普通对象或数组不一样。

要回答你的问题如何遍历:

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

回答如何将这些转换为数组:

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

由于这仍然给你SimpleXMLElements,但你可能希望有值你可以这些元素转换为字符串上使用,例如:

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

或整个数组到字符串转换:

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

或可替代成整数:

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

+1没有意识到'array_map()'会抱怨:) –

0

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

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

$ value then = SimpleXMLElement Object([0] => 65233)SimpleXMLElement Object([0] => 65234)我试图从对象echo $ value [0]中获取值[0];返回null – Philip

+0

@Philip:是的,因为'[0]'是虚拟的。 'print_r'在这里欺骗了你,因为这个魔法对'SimpleXMLElement'特别有用。看到我的答案http://stackoverflow.com/a/13999675/367456 – hakre

+0

@hakre你已经扩展了我对SimpleXMLElement的知识。谢谢。 – Philip

1

,就可以把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; 
} 

或铸整个事情:

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

更新

还行,array_map代码只是上面并没有真正的工作,因为它不是严格意义上的数组,你应该申请iterator_to_array($message_object->data_id, false)第一:

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

参见:@hakre's answer

+0

@hakre谢谢,你的评论已经合并:) –

+0

'$ message_object-> data-> id'不是一个数组,所以'array_map'会抱怨这个。首先在这里使用没有键的'iterator_to_array',看到我的答案http://stackoverflow.com/a/13999675/367456 – hakre