【问题标题】:PHP - Merge two arrays of objects included other objectsPHP - 合并两个对象数组,包括其他对象
【发布时间】:2020-03-22 07:53:04
【问题描述】:

在我的 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() 返回一个对象数组(它们本身包含对象)。

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

【问题讨论】:

  • 每个id在任何数组中只能出现一次吗?
  • 是的,但最后,我可以有很多次相同的标识符,所以我必须删除重复项。 (对不起我是法国人的英语)
  • array_column(array, null,'id') 将 id 作为数组的索引。比你可以通过通常的循环合并它们

标签: php arrays symfony object merge


【解决方案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);

【讨论】:

  • 我更新了我的第一篇文章,我认为我使用你的代码很糟糕
  • 只要id是唯一的,就应该没有重复,$tabDemandes怎么样,可以贴一下吗?
【解决方案2】:

你可以这样做:

$result = [];

$array1 = [
  [
    "id" => 1,
    "other_data" => "text1"
  ],
  [
    "id" => 2,
    "other_data" => "text2"
  ],
  [
    "id" => 3,
    "other_data" => "text3"
  ],  
];

$array2 = [
  [
    "id" => 1,
    "other_data" => "text1"
  ],
  [
    "id" => 8,
    "other_data" => "text8"
  ],
];

$merge_array = function($array, $result) {
  foreach($array as $element) {
      $id = $element["id"]; //In your case it could be $element->getId()
      if(!isset($result[$id])) { 
         $result[$id] = $element;
      }
  }

  return $result;
};

$result = $merge_array($array1, $result);
$result = $merge_array($array2, $result);
print_r(array_values($result));

【讨论】:

    猜你喜欢
    • 2019-05-20
    • 2015-01-26
    • 1970-01-01
    • 2017-01-17
    • 2021-03-08
    • 1970-01-01
    • 2020-02-04
    • 2016-06-10
    • 2022-01-16
    相关资源
    最近更新 更多