【问题标题】:array unique and merge value in an array of objects数组唯一并合并对象数组中的值
【发布时间】:2016-06-22 09:59:50
【问题描述】:

我有一个大数组,我想让它在 doc_id 上唯一并合并 roleid

 Array
(
    [0] => stdClass Object
        (
            [title] => Test ÆØÅ
            [doc_id] => 279
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  76, 276
        )

    [1] => stdClass Object
        (
            [title] => Test ÆØÅ
            [doc_id] => 279
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  76, 276
        )

    [2] => stdClass Object
        (
            [title] => NVD AS
            [doc_id] => 415
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  2
        )

    [3] => stdClass Object
        (
            [title] => NVD AS
            [doc_id] => 415
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  76, 276
        )

    [4] => stdClass Object
        (
            [title] => Test for SN
            [doc_id] => 506
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  76, 276
        )

    [5] => stdClass Object
        (
            [title] => Test for SN
            [doc_id] => 506
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  6
        )

为了让它独一无二,我使用下面的代码

foreach ($arry as $val) {
            $newArr[$arry ->doc_id] = $val;
        }
        $result = array_values($newArr);

我可以获得唯一的文档,但我丢失了 roleid,我需要将 roleid 与 doc_id 合并 应该是

Array
(
    [0] => stdClass Object
        (
            [title] => Test ÆØÅ
            [doc_id] => 279
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  76, 276
        )

    [1] => stdClass Object
        (
            [title] => NVD AS
            [doc_id] => 415
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  2,76, 276
        )

    [2] => stdClass Object
        (
            [title] => Test for SN
            [doc_id] => 506
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] => 6,76, 276
        )
Can anyone help in this

提前致谢

【问题讨论】:

  • 以上不是多维数组,而是对象数组。

标签: php arrays multidimensional-array merge unique


【解决方案1】:

您当前的roleid 格式不是处理的最佳格式。如果将其更改为数组,则可以执行类似的操作:

$item1 = new stdClass();
$item1->doc_id = 279;
$item1->role_id = '76, 276, 3';

$item2 = new stdClass();
$item2->doc_id = 279;
$item2->role_id = '1, 44, 76';

$item3 = new stdClass();
$item3->doc_id = 279;
$item3->role_id = '8, 44, 1';

$data = [$item1, $item2, $item3];

$result = [];
foreach ($data as $item) {
    if (isset($result[$item->doc_id])) {
        $existingIds = explode(', ', $result[$item->doc_id]->role_id);
        $newIds = explode(', ', $item->role_id);

        $result[$item->doc_id]->role_id = implode(', ', array_unique(array_merge(
            $existingIds,
            $newIds
        )));

        continue;
    }

    $result[$item->doc_id] = $item;
}


print_r($result);

结果:

Array
(
    [279] => stdClass Object
        (
            [doc_id] => 279
            [role_id] => 76, 276, 3, 1, 44, 8
        )

)

【讨论】:

  • 但我在查询中将这个角色标识作为逗号分隔值。我怎样才能改变它
  • 您可以使用implode()explode()函数在数组合并之前将字符串转换为数组,然后再转换回字符串。
  • 我厌倦了像这样爆炸角色,但后来我得到了 array_merge(): Argument #1 is not an array $roleid = explode(',', $item->roleid); $result[$item->doc_id]->roleid = array_merge($result[$item->doc_id]->roleid, $roleid);
  • 谢谢,现在它正在合并,但会产生一些奇怪的结果。像 [roleid] => 191, 231, 245, 191, 231, 245, 191, 231, 245, 191, 245, 191, 245, 231
  • 多次合并值
【解决方案2】:

您可以尝试使用array_merge_recursive() 函数合并您的数组。

$mergedArray = array_merge_recursive($arr1, $arr2);

编辑

您正在丢失值,因为您覆盖了它们。你应该使用两个循环。

foreach ($array as $key => $value) {
    foreach ($value as $subkey => $subvalue) {
        $newArray[$value->doc_id][$subkey] = $subvalue;
    }
}

我似乎找不到上述方法对您不起作用的原因。虽然我会给你写另一个解决方案:

$newaArray = array();
foreach ($array as $value) {
    if (!is_array($newArray[$value->doc_id])) {
        $newArray[$value->doc_id] = array();
    }
    $newArray[$value->doc_id] = array_merge($newArray[$value->doc_id], $value);
}

资源

【讨论】:

  • 如果我使用 array_merge_recursive() 那么我将丢失唯一的数组;
  • not work 对我来说很难做些什么。如果你使用我的 foreach 会发生什么?
  • 相同的数组具有唯一值,但没有合并角色 ID
猜你喜欢
  • 1970-01-01
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-27
  • 2018-08-15
  • 1970-01-01
相关资源
最近更新 更多