developer tip

PHP-두 배열을 하나의 배열로 병합 (중복 제거)

optionbox 2020. 8. 23. 08:59
반응형

PHP-두 배열을 하나의 배열로 병합 (중복 제거)


안녕하세요, 두 개의 배열을 병합하고 최종 배열에서 중복 값을 제거하려고합니다.

다음은 내 어레이 1입니다.

Array
    (
    [0] => stdClass Object
    (
    [ID] => 749
    [post_author] => 1
    [post_date] => 2012-11-20 06:26:07
    [post_date_gmt] => 2012-11-20 06:26:07
)

그리고 이것은 내 배열 2입니다.

Array
(
[0] => stdClass Object
(
[ID] => 749
[post_author] => 1
[post_date] => 2012-11-20 06:26:07
[post_date_gmt] => 2012-11-20 06:26:07

)

array_merge두 배열을 하나의 배열로 병합하는 데 사용 하고 있습니다. 다음과 같은 출력을 제공합니다.

Array
(
[0] => stdClass Object
(
[ID] => 749
[post_author] => 1
[post_date] => 2012-11-20 06:26:07
[post_date_gmt] => 2012-11-20 06:26:07

[1] => stdClass Object
(
[ID] => 749
[post_author] => 1
[post_date] => 2012-11-20 06:26:07
[post_date_gmt] => 2012-11-20 06:26:07

)

중복 된 항목을 제거하거나 병합하기 전에 제거 할 수 있습니까? 도와주세요 .. 감사합니다 !!!!!!!


array_unique(array_merge($array1,$array2), SORT_REGULAR);

http://se2.php.net/manual/en/function.array-unique.php


이미 언급했듯이 array_unique ()를 사용할 수 있지만 단순한 데이터를 다룰 때만 사용할 수 있습니다. 개체는 처리하기가 그렇게 간단하지 않습니다.

PHP는 배열을 병합하려고 할 때 배열 구성원의 값을 비교하려고합니다. 멤버가 개체 인 경우 값을 가져올 수 없으며 대신 spl 해시를 사용합니다. 여기에서 spl_object_hash에 대해 자세히 알아보십시오.

두 개의 객체, 매우 동일한 클래스의 인스턴스가 있고 그중 하나가 다른 하나에 대한 참조가 아닌 경우 간단히 말하면 속성 값에 관계없이 두 개의 객체를 갖게됩니다.

병합 된 배열 내에 중복 항목이 없는지 확인하려면 Imho가 직접 케이스를 처리해야합니다.

또한 다차원 배열을 병합하려는 경우 array_merge () 보다 array_merge_recursive () 사용을 고려하십시오 .


두 배열을 병합하고 중복을 제거합니다.

<?php
 $first = 'your first array';
 $second = 'your second array';
 $result = array_merge($first,$second);
 print_r($result);
 $result1= array_unique($result);
 print_r($result1);
 ?>

이 링크를 시도 link1


사용하려고 array_unique()

this elminates duplicated data inside the list of your arrays..


Merging two array will not remove the duplicate you can try the below example to get unique from two array

$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_diff($a1,$a2);
print_r($result);

참고URL : https://stackoverflow.com/questions/13469803/php-merging-two-arrays-into-one-array-also-remove-duplicates

반응형