I’m hitting a wall in PHP and could use your help.
# 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.
I have the following code:
$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)
);
}
It returns the following:
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
Somehow, the data fields seem to have lost their Array nature. I can’t figure out why this is… can anyone point me in the right direction here, please?
Update
Even stranger. The following code:
$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";
Returns
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
So, I can run foreach against it, but if I dump it, I only get the first element… is this normal?
SimpleXML uses a lot of magic overloading. In some cases, this is not entirely transparent to PHP. For example, if an object implements the
Traversableinterface, it’s traversable usingforeach, but does not in any other respect behave like an array. I don’t know the exact implementation of the SimpleXMLElement in this case, but I’m sure you’re bumping up against something like this.For example,
SimpleXMLElement::childrencites this as a usage example:while the method signature is
with this note:
So,
->datais not actually an array. It just behaves like one under certain circumstances and poses as one for simplicity. In fact, it’s not even a real property of the object, but something returned via__get.