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

新手上路,请多包涵

我有以下两个对象数组:

第一个数组: $array1

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

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

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

第二个数组: $array2

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

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

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

期望的输出:

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

原文由 M. Ahmad Zafar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 506
2 个回答

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

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

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

原文由 M. Ahmad Zafar 发布,翻译遵循 CC BY-SA 3.0 许可协议

更新

我现在在这里发布整个代码清单而不是之前发布的主要代码,同时打印输入和输出。您可以简单地复制并粘贴此代码进行测试。

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

它将输出以下内容:

 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
        )

)

原文由 Software Guy 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏