合并两个对象数组并获取PHP的唯一对象

时间:2015-12-02 10:55:18

标签: php arrays codeigniter

这里我需要一些关于合并两个数组的帮助。

这是我的示例代码

    $tasks = array();
    $task_user = new Task_User;
    $task_user->setUserId($this->session_data['userId']);
    $user_taskIds = $this->getUserTasks($task_user);
    foreach ($user_taskIds as $row) {
        $task = new Task;
        // $task->setBranchId($branch_id);
        $task->setTaskId($row->getTaskId());
        $tasks[] = $this->getTask($task);
    }
    echo "<pre>";
    print_r($tasks);
    $pageData['userTasks'] = $tasks;
    $task = new Task;
    // $task->setBranchId($branch_id);
    $task->setCreated($this->session_data['userId']);
    $tasks1 = $this->getBranchTasks($task);
    if (!empty($tasks1[0]))
    {
        $pageData['userTasks'] = $tasks1;
    }
    print_r($tasks1);
    print_r($pageData['userTasks']);

$ this-&gt; load-&gt; view(&#39; home&#39;,$ pageData);

输出:

Array
(
    [0] => Task Object
        (
            [task_id:Task:private] => 22
            [service_id:Task:private] => 1
            [branch_id:Task:private] => 1
            [customer_id:Task:private] => 121
            [lead_id:Task:private] => 0
            [name:Task:private] => YTY0000022
        )

    [1] => Task Object
        (
            [task_id:Task:private] => 24
            [service_id:Task:private] => 1
            [branch_id:Task:private] => 1
            [customer_id:Task:private] => 1046
            [lead_id:Task:private] => 0
            [name:Task:private] => YTY0000024
        )

)
Array
(
    [0] => Task Object
        (
            [task_id:Task:private] => 9
            [service_id:Task:private] => 1
            [branch_id:Task:private] => 1
            [customer_id:Task:private] => 599
            [lead_id:Task:private] => 0
            [name:Task:private] => YTY0000009
        )

    [1] => Task Object
        (
            [task_id:Task:private] => 10
            [service_id:Task:private] => 1
            [branch_id:Task:private] => 1
            [customer_id:Task:private] => 313
            [lead_id:Task:private] => 0
            [name:Task:private] => YTY0000010
        )

)
Array
(
    [0] => Task Object
        (
            [task_id:Task:private] => 9
            [service_id:Task:private] => 1
            [branch_id:Task:private] => 1
            [customer_id:Task:private] => 599
            [lead_id:Task:private] => 0
            [name:Task:private] => YTY0000009
        )

    [1] => Task Object
        (
            [task_id:Task:private] => 10
            [service_id:Task:private] => 1
            [branch_id:Task:private] => 1
            [customer_id:Task:private] => 313
            [lead_id:Task:private] => 0
            [name:Task:private] => YTY0000010
        )

)

所以,最后我需要两个对象数组应该与唯一对象组合并将其传递给一个数组。我从最近6个小时开始尝试了。但我无法得到它。请帮帮我 在此先感谢。

2 个答案:

答案 0 :(得分:0)

在一些简短的测试中,看起来比in_array与对象一起使用。然而,它不能观察私有变量,而不会揭露它们,这是我留给你的一个练习。

$tasks2 = [];
foreach($tasks as $t){
    if(!in_array($t,$tasks2){
        array_push($tasks2,$t);
    }
}

答案 1 :(得分:0)

替换此行     ...

if (!empty($tasks1[0]))
{
    $pageData['userTasks'] = $tasks1;
}
...

成     ...

if (!empty($tasks1[0]))
{
    $pageData['userTasks'] = array_replace($pageData['userTasks'], $tasks1);
}
...