15

我使用一个 API 将我返回到给定这样一个数组的电子邮件地址:

标准类对象
(
    [状态] => 好的
    [联系方式] => 标准类对象
        (
            [id] => 0000000
            [电子邮件] => toto@free.fr
            [last_activity] => 1362131446
            [last_update] => 0
            [created_at] => 1356617740
            [发送] => 5
            [打开] => 1
            [点击] => 1
            [垃圾邮件] => 0
            [反弹] => 0
            [阻塞] => 0
            [排队] => 0
        )
[列表] => 数组
        (
            [0] => 标准类对象
                (
                    [活跃] => 1
                    [取消订阅] => 1
                    [unsub_at] => 1363078528
                )

        )

)

如何在单个对象中将信息 [联系人] 与 [列表] [0] 合并?

感谢您的帮助

4

3 回答 3

22
$info = yourstuff;
$arrContact = (array) $info->contact;
$arrList = (array) $info->lists[0];
$merged = array_merge($arrContact, $arrList);
var_dump($merged, 'have fun');

相当琐碎;)

于 2013-03-18T09:26:00.560 回答
11
 <?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"
    }
*/ 
于 2013-03-18T09:30:54.597 回答
5

怎么样:

foreach ($info->lists[0] as $key => $value {
  $info->contact->$key = $value;
}

避免在数组和对象之间来回转换

于 2014-11-14T11:17:42.917 回答