为什么对象字段会失去其数组特性

时间:2011-07-24 22:11:01

标签: php arrays

我正在用PHP打墙,可以使用你的帮助。

# php -v
PHP 5.3.2 (cli) (built: Jun  4 2010 19:29:54) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with the ionCube PHP Loader v3.3.20, Copyright (c) 2002-2010, by ionCube Ltd.

我有以下代码:

$forwards  = $xmlapi->api2_query($account, "Email", "listforwards");
var_dump($forwards);
$mailboxes = $xmlapi->api2_query($account, "Email", "listpops");
var_dump($mailboxes);
$emails    = array();
if($mailboxes->event->result != 1) {
    wp_die( __('Not able to fetch full NN email list: '.$mailboxes->event->reason));
}
elseif($forwards->event->result != 1) {
    wp_die( __('Not able to fetch full NN email list: '.$forwards->event->reason));
}
else {
    $extract_forward_emails = create_function('$f','return $f->dest;');
    $extract_mailbox_emails = create_function('$f','return $f->email;');

    $emails = array_merge(
        array_map($extract_mailbox_emails, $mailboxes->data),
        array_map($extract_forward_emails, $forwards->data)
    );
}

返回以下内容:

object(SimpleXMLElement)#121 (5) {
  ["apiversion"]=>
  string(1) "2"
  ["data"]=>
  array(60) {
    [0]=>
    object(SimpleXMLElement)#182 (6) {
      ["dest"]=>
      string(17) "xxxxxxx"
      ["forward"]=>
      string(20) "xxxxxxx"
      ["html_dest"]=>
      string(17) "xxxxxxx"
      ["html_forward"]=>
      string(20) "xxxxxxx"
      ["uri_dest"]=>
      string(19) "xxxxxxx"
      ["uri_forward"]=>
      string(22) "adam.brisk%40gmail.com"
    }
    [1]=>
    object(SimpleXMLElement)#181 (6) {...}
    ...
  }
  ["event"]=>
  object(SimpleXMLElement)#122 (1) {
    ["result"]=>
    string(1) "1"
  }
  ["func"]=>
  string(12) "listforwards"
  ["module"]=>
  string(5) "Email"
}
object(SimpleXMLElement)#122 (5) {
  ["apiversion"]=>
  string(1) "2"
  ["data"]=>
  array(28) {
    [0]=>
    object(SimpleXMLElement)#151 (2) {
      ["email"]=>
      string(19) "xxxxxxx"
      ["login"]=>
      string(19) "xxxxxxx"
    }
    [1]=>
    object(SimpleXMLElement)#181 (2) {...}
    ...
  }
  ["event"]=>
  object(SimpleXMLElement)#123 (1) {
    ["result"]=>
    string(1) "1"
  }
  ["func"]=>
  string(8) "listpops"
  ["module"]=>
  string(5) "Email"
}


Warning:  array_map() [function.array-map]: Argument #2 should be an array in xxxxxx on line 61

Warning:  array_map() [function.array-map]: Argument #2 should be an array in xxxxxx on line 62

Warning:  array_merge() [function.array-merge]: Argument #1 is not an array in xxxxxx on line 63

不知何故,data字段似乎已经失去了它们的数组性质。我无法弄清楚为什么会这样......有人能指出我在正确的方向吗?

更新

更奇怪的是。以下代码:

$forwards = $xmlapi->api2_query($account, "Email", "listforwards");
print "Result: ".$forwards->event->result;
print "\n";
var_dump($forwards->data);
foreach ($forwards->data as $forward) {
  print $forward->dest."\n";
}
print "\nCompleted\n";

返回

Result: 1
object(SimpleXMLElement)#5 (6) {
  ["dest"]=>
  string(17) "xxxxxx"
  ["forward"]=>
  string(20) "xxxxxx"
  ["html_dest"]=>
  string(17) "xxxxxx"
  ["html_forward"]=>
  string(20) "xxxxxx"
  ["uri_dest"]=>
  string(19) "xxxxxx"
  ["uri_forward"]=>
  string(22) "xxxxxx"
}
xxxxxx
yyyyyy
.
.
.
zzzzzz

Completed

所以,我可以对它运行foreach,但是如果我转储它,我只得到第一个元素......这是正常的吗?

1 个答案:

答案 0 :(得分:1)

SimpleXML使用了大量magic重载。在某些情况下,这对PHP来说并不完全透明。例如,如果一个对象实现了Traversable接口,那么它可以使用foreach遍历,但在任何其他方面都不像数组那样。在这种情况下,我不知道SimpleXMLElement的确切实现,但我确信你会碰到这样的事情。

例如,SimpleXMLElement::children引用此作为用法示例:

foreach ($xml->children() as $second_gen)

虽然方法签名是

public SimpleXMLElement SimpleXMLElement::children ([ string $ns [, bool $is_prefix = false ]] )

有这个说明:

  

SimpleXML制定了向大多数方法添加迭代属性的规则。无法使用var_dump()或其他任何可以检查对象的内容查看它们。

所以,->data实际上并不是一个数组。它在某些情况下表现得像一个,并且为了简单而构成一个。事实上,它甚至不是对象的真实属性,而是通过__get返回的东西。