亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP - 合并兩個對象數組,包括其他對象

PHP - 合并兩個對象數組,包括其他對象

PHP
MMMHUHU 2022-05-27 09:53:48
在我的 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        )我使用這段代碼來做到這一點:$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。是因為我的代碼嗎?
查看完整描述

2 回答

?
手掌心

TA貢獻1942條經驗 獲得超3個贊

根據 splash58 的評論,您可以使用array_column()然后合并數組,同時只保留第一項??梢栽谶@里看到一個樣本。


<?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);


查看完整回答
反對 回復 2022-05-27
?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

你可以這樣做:


$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));


查看完整回答
反對 回復 2022-05-27
  • 2 回答
  • 0 關注
  • 119 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號