如何在PHP中合并两个对象数组

时间:2022-06-27 09:08:14

I have the following two arrays of objects:

我有以下两个对象数组:

First Array: $array1

第一个数组:$ array1

Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => Muhammad
        )

    [1] => stdClass Object
        (
            [id] => 102
            [name] => Ibrahim
        )

    [2] => stdClass Object
        (
            [id] => 101
            [name] => Sumayyah
        )
)

Second Array: $array2

第二个数组:$ array2

Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => Muhammad
        )

    [1] => stdClass Object
        (
            [id] => 103
            [name] => Yusuf
        )
)

I want to merge these two object arrays (removing all duplicates) and sorted according to id.

我想合并这两个对象数组(删除所有重复项)并根据id进行排序。

Desired output:

期望的输出:

Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => Muhammad
        )

    [1] => stdClass Object
        (
            [id] => 101
            [name] => Sumayyah
        )

    [2] => stdClass Object
        (
            [id] => 102
            [name] => Ibrahim
        )

    [3] => stdClass Object
        (
            [id] => 103
            [name] => Yusuf
        )
)

3 个解决方案

#1


26  

These 3 simple steps did the work:

这三个简单的步骤完成了工作:

//both arrays will be merged including duplicates
$result = array_merge( $array1, $array2 );
//duplicate objects will be removed
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));
//array is sorted on the bases of id
sort( $result );

Note: Answer by @Kamran helped me come to this simple solution

注意:@Kamran的回答帮助我找到了这个简单的解决方案

#2


4  

UPDATE

UPDATE

I am posting the entire code listing here now instead of the previously posted main code, printing both input and output. You can simply copy and paste this code to test.

我现在在这里发布整个代码列表而不是之前发布的主代码,打印输入和输出。您只需复制并粘贴此代码即可进行测试。

<?php

function array_to_object($arr) {
    $arrObject = array();
    foreach ($arr as $array) {
        $object = new stdClass();
        foreach ($array as $key => $value) {
            $object->$key = $value;
        }
        $arrObject[] = $object;
    }

    return $arrObject;
}

function super_unique($array)
{
    $result = array_map("unserialize", array_unique(array_map("serialize", $array)));
    foreach ($result as $key => $value)  {
        if ( is_array($value) ) {
          $result[$key] = super_unique($value);
        }
    }
    return $result;
}

function merge_arrays($arr1, $arr2) {
    $arr1 = (array)$arr1;
    $arr2 = (array)$arr2;
    $output = array_merge($arr1, $arr2);
    sort($output);
    return super_unique($output);
}

$array1 = array(
        array("id" => "100", "name" => "muhammad"), 
        array("id" => "102", "name" => "ibrahim"), 
        array("id" => "101", "name" => "summayyah"), 
    );
$array1 = array_to_object($array1);

print "<h3>Your array 1</h3>";
print "<pre>";
print_r($array1);
print "</pre>";

$array2 = array(
        array("id" => "100", "name" => "muhammad"), 
        array("id" => "103", "name" => "yusuf"), 
    );
$array2 = array_to_object($array2);

print "<h3>Your array 2</h3>";
print "<pre>";
print_r($array2);
print "</pre>";

$result = merge_arrays($array1, $array2);

print "<h3>Your desired output</h3>";
print "<pre>";
print_r($result);
print "</pre>";

it will output the following:

它将输出以下内容:

Your array 1
Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => muhammad
        )

    [1] => stdClass Object
        (
            [id] => 102
            [name] => ibrahim
        )

    [2] => stdClass Object
        (
            [id] => 101
            [name] => summayyah
        )

)

Your array 2
Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => muhammad
        )

    [1] => stdClass Object
        (
            [id] => 103
            [name] => yusuf
        )

)

Your desired output
Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => muhammad
        )

    [2] => stdClass Object
        (
            [id] => 101
            [name] => summayyah
        )

    [3] => stdClass Object
        (
            [id] => 102
            [name] => ibrahim
        )

    [4] => stdClass Object
        (
            [id] => 103
            [name] => yusuf
        )

)

#3


0  

Assignments:

作业:

  1. Merge
  2. 合并
  3. Remove Duplicates
  4. 删除重复项
  5. Sort by id
  6. 按ID排序

The good news is: Assigning temporary keys using id values does all of the hard work for you. No serializing is needed.

好消息是:使用id值分配临时密钥可以帮助您完成所有工作。不需要序列化。

  • array_merge() joins the arrays together.
  • array_merge()将数组连接在一起。
  • array_column() with a null 2nd parameter leaves the objects unmodified and id as the 3rd parameter assigns the temporary keys. Because arrays cannot have duplicate keys, duplicate objects are weeded out in this step.
  • 带有null第二个参数的array_column()使对象保持不变,而id作为第3个参数指定临时键。由于数组不能具有重复键,因此在此步骤中将删除重复的对象。
  • Now that we have keys, ksort() avoids calling the more convoluted usort() to sort by id ascending.
  • 现在我们有了密钥,ksort()避免调用更复杂的usort()来按id递增排序。
  • Finally, call array_values() to re-index the resultant array (remove the temporary keys).
  • 最后,调用array_values()重新索引结果数组(删除临时键)。

Code: (Demo)

代码:(演示)

$array1 = [
    (object) ['id' => 100, 'name' => 'Muhammad'],
    (object) ['id' => 102, 'name' => 'Ibrahim'],
    (object) ['id' => 101, 'name' => 'Sumayyah']
];

$array2 = [
    (object) ['id' => 100, 'name' => 'Muhammad'],
    (object) ['id' => 103, 'name' => 'Yusuf']
];

$merged_keyed = array_column(array_merge($array1,$array2), NULL, 'id');
ksort($merged_keyed);
print_r(array_values($merged_keyed));

Output:

输出:

Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => Muhammad
        )    
    [1] => stdClass Object
        (
            [id] => 101
            [name] => Sumayyah
        )    
    [2] => stdClass Object
        (
            [id] => 102
            [name] => Ibrahim
        )    
    [3] => stdClass Object
        (
            [id] => 103
            [name] => Yusuf
        )    
)

#1


26  

These 3 simple steps did the work:

这三个简单的步骤完成了工作:

//both arrays will be merged including duplicates
$result = array_merge( $array1, $array2 );
//duplicate objects will be removed
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));
//array is sorted on the bases of id
sort( $result );

Note: Answer by @Kamran helped me come to this simple solution

注意:@Kamran的回答帮助我找到了这个简单的解决方案

#2


4  

UPDATE

UPDATE

I am posting the entire code listing here now instead of the previously posted main code, printing both input and output. You can simply copy and paste this code to test.

我现在在这里发布整个代码列表而不是之前发布的主代码,打印输入和输出。您只需复制并粘贴此代码即可进行测试。

<?php

function array_to_object($arr) {
    $arrObject = array();
    foreach ($arr as $array) {
        $object = new stdClass();
        foreach ($array as $key => $value) {
            $object->$key = $value;
        }
        $arrObject[] = $object;
    }

    return $arrObject;
}

function super_unique($array)
{
    $result = array_map("unserialize", array_unique(array_map("serialize", $array)));
    foreach ($result as $key => $value)  {
        if ( is_array($value) ) {
          $result[$key] = super_unique($value);
        }
    }
    return $result;
}

function merge_arrays($arr1, $arr2) {
    $arr1 = (array)$arr1;
    $arr2 = (array)$arr2;
    $output = array_merge($arr1, $arr2);
    sort($output);
    return super_unique($output);
}

$array1 = array(
        array("id" => "100", "name" => "muhammad"), 
        array("id" => "102", "name" => "ibrahim"), 
        array("id" => "101", "name" => "summayyah"), 
    );
$array1 = array_to_object($array1);

print "<h3>Your array 1</h3>";
print "<pre>";
print_r($array1);
print "</pre>";

$array2 = array(
        array("id" => "100", "name" => "muhammad"), 
        array("id" => "103", "name" => "yusuf"), 
    );
$array2 = array_to_object($array2);

print "<h3>Your array 2</h3>";
print "<pre>";
print_r($array2);
print "</pre>";

$result = merge_arrays($array1, $array2);

print "<h3>Your desired output</h3>";
print "<pre>";
print_r($result);
print "</pre>";

it will output the following:

它将输出以下内容:

Your array 1
Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => muhammad
        )

    [1] => stdClass Object
        (
            [id] => 102
            [name] => ibrahim
        )

    [2] => stdClass Object
        (
            [id] => 101
            [name] => summayyah
        )

)

Your array 2
Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => muhammad
        )

    [1] => stdClass Object
        (
            [id] => 103
            [name] => yusuf
        )

)

Your desired output
Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => muhammad
        )

    [2] => stdClass Object
        (
            [id] => 101
            [name] => summayyah
        )

    [3] => stdClass Object
        (
            [id] => 102
            [name] => ibrahim
        )

    [4] => stdClass Object
        (
            [id] => 103
            [name] => yusuf
        )

)

#3


0  

Assignments:

作业:

  1. Merge
  2. 合并
  3. Remove Duplicates
  4. 删除重复项
  5. Sort by id
  6. 按ID排序

The good news is: Assigning temporary keys using id values does all of the hard work for you. No serializing is needed.

好消息是:使用id值分配临时密钥可以帮助您完成所有工作。不需要序列化。

  • array_merge() joins the arrays together.
  • array_merge()将数组连接在一起。
  • array_column() with a null 2nd parameter leaves the objects unmodified and id as the 3rd parameter assigns the temporary keys. Because arrays cannot have duplicate keys, duplicate objects are weeded out in this step.
  • 带有null第二个参数的array_column()使对象保持不变,而id作为第3个参数指定临时键。由于数组不能具有重复键,因此在此步骤中将删除重复的对象。
  • Now that we have keys, ksort() avoids calling the more convoluted usort() to sort by id ascending.
  • 现在我们有了密钥,ksort()避免调用更复杂的usort()来按id递增排序。
  • Finally, call array_values() to re-index the resultant array (remove the temporary keys).
  • 最后,调用array_values()重新索引结果数组(删除临时键)。

Code: (Demo)

代码:(演示)

$array1 = [
    (object) ['id' => 100, 'name' => 'Muhammad'],
    (object) ['id' => 102, 'name' => 'Ibrahim'],
    (object) ['id' => 101, 'name' => 'Sumayyah']
];

$array2 = [
    (object) ['id' => 100, 'name' => 'Muhammad'],
    (object) ['id' => 103, 'name' => 'Yusuf']
];

$merged_keyed = array_column(array_merge($array1,$array2), NULL, 'id');
ksort($merged_keyed);
print_r(array_values($merged_keyed));

Output:

输出:

Array
(
    [0] => stdClass Object
        (
            [id] => 100
            [name] => Muhammad
        )    
    [1] => stdClass Object
        (
            [id] => 101
            [name] => Sumayyah
        )    
    [2] => stdClass Object
        (
            [id] => 102
            [name] => Ibrahim
        )    
    [3] => stdClass Object
        (
            [id] => 103
            [name] => Yusuf
        )    
)