我使用API将我返回到一个电子邮件地址,给出如下数组:
stdClass Object ( [status] => OK [contact] => stdClass Object ( [id] => 0000000 [email] => toto@free.fr [last_activity] => 1362131446 [last_update] => 0 [created_at] => 1356617740 [sent] => 5 [open] => 1 [click] => 1 [spam] => 0 [bounce] => 0 [blocked] => 0 [queued] => 0 ) [lists] => Array ( [0] => stdClass Object ( [active] => 1 [unsub] => 1 [unsub_at] => 1363078528 ) ) )
如何在单个对象中将[联系人]与[list] [0]合并?
感谢您的帮助
答案 0 :(得分:22)
$info = yourstuff;
$arrContact = (array) $info->contact;
$arrList = (array) $info->lists[0];
$merged = array_merge($arrContact, $arrList);
var_dump($merged, 'have fun');
相当简单;)
答案 1 :(得分:10)
<?php
$a = new stdClass;
$a->name = 'Anthony';
$b = new stdClass;
$b->location = 'UK';
$c = (object)array_merge((array)$a, (array)$b);
var_dump($c);
/*
object(stdClass)#3 (2) {
["name"]=>
string(7) "Anthony"
["location"]=>
string(2) "UK"
}
*/
答案 2 :(得分:3)
怎么样:
foreach ($info->lists[0] as $key => $value {
$info->contact->$key = $value;
}
避免在数组和对象之间来回转换