PHP-合并包含其他对象的两个对象数组

时间:2019-11-26 20:09:03

标签: php arrays symfony object merge

在我的symfony项目中,我需要能够合并多个对象数组,同时删除重复项。

例如:

数组1:

Array
(
    [0] => Absence Object
        (
            [id] => 1
            [type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
            [user] => User Object (......)
            [debut] => 12-11-2019 00:00:00
        )

    [1] => Absence Object
        (
            [id] => 2
            [type] => TypeConge Object ([id] => 5, [nom] => "CA")
            [user] => User Object (......)
            [debut] => 13-11-2019 00:00:00
        )

    [2] => Absence Object
        (
            [id] => 3
            [type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
            [user] => User Object (......)
            [debut] => 11-11-2019 00:00:00
        )    
)

数组2:

Array
(
    [0] => Absence Object
        (
            [id] => 1
            [type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
            [user] => User Object (......)
            [debut] => 12-11-2019 00:00:00
        )

    [1] => Absence Object
        (
            [id] => 8
            [type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
            [user] => User Object (......)
            [debut] => 17-11-2019 00:00:00
        )    
)

输出:

    Array
(
    [0] => Absence Object
        (
            [id] => 1
            [type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
            [user] => User Object (......)
            [debut] => 12-11-2019 00:00:00
        )

    [1] => Absence Object
        (
            [id] => 2
            [type] => TypeConge Object ([id] => 5, [nom] => "CA")
            [user] => User Object (......)
            [debut] => 13-11-2019 00:00:00
        )

    [2] => Absence Object
        (
            [id] => 3
            [type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
            [user] => User Object (......)
            [debut] => 11-11-2019 00:00:00
        )    
    [3] => Absence Object
        (
            [id] => 8
            [type] => TypeConge Object ([id] => 4, [nom] => "Maladie")
            [user] => User Object (......)
            [debut] => 17-11-2019 00:00:00
        )    

)

我用这段代码来做:

$demandes = $this->getDemandesValidateur($unUser, $search, $superValidation);
$tabDemandes = array_merge($tabDemandes, $demandes);
$tabDemandes = array_map("unserialize", array_unique(array_map("serialize", $tabDemandes)));

但是我给人的印象是它由于序列化和反序列化而导致错误。我获得了不错的决赛桌,但有些数据似乎无法使用。例如,我无法致电:

$absence->getType()->getNom();

有时返回null。是因为我的代码吗?

我的案子:

要弄清楚我的情况,这是我应该做的:

我一开始有个空板。 然后,执行循环以检索合并的其他表。最后,我删除了重复项。

它看起来像这样:

                case "ROLE_SUPPLEANT":
                    $tabDemandes = [];
                    $users = $this->repoUsers->getUsersWhereIsSuppleant($user);

                    foreach($users as $unUser)
                    {
                        $demandes = array_column($this->getDemandesCongesForCalendar($unUser, $demandesConges, "ROLE_VALIDATEUR"), null, 'id');
                        $tabDemandes = $tabDemandes + $demandes;
                    }
                    if($user->hasRole("ROLE_VALIDATEUR"))
                    {
                        $demandes = array_column($this->getDemandesCongesForCalendar($user, $demandesConges, "ROLE_VALIDATEUR"), null, 'id');
                        $tabDemandes = $tabDemandes + $demandes;
                    }
                    break;

知道函数getDemandesCongesForCalendar()返回一个对象数组(它们本身包含对象)。

我的印象是滥用了您建议的代码,因为它不会删除我认为最后的重复代码。 在任何情况下,变量$ request将包含具有唯一值的数组,不会有重复项。 但是由于每次我都将其值添加到通用表($ tabDemands)中,因此后者可能有重复项。这就是我应该删除重复项的地方

2 个答案:

答案 0 :(得分:1)

基于splash58的注释,可以使用array_column(),然后合并数组,同时仅保留第一项。可以看到一个示例here

<?php

$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);

$records2 = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5624,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);

$first = array_column($records, null, 'id');
$second = array_column($records2, null, 'id');
$third = $first + $second;

// if you want to reset indexes
// $third = array_values($third);

echo print_r($third);

答案 1 :(得分:0)

您可以执行以下操作:

inputID