PHP合并具有相同键/值的数组

时间:2018-09-20 20:27:07

标签: php arrays

如果Im希望将此PHP数组合并为一个,成为array(1649)中最低的ID。这样我只会看到

  array:1 [
    1649 => array:2 [
      "firstName"   => "jack"
      "lastName"    => "straw"
      "mergedWith"  array:3 [
         "id" =>'1650'
         "id" =>'1651'
         "id" =>'1652'
      ]
    ]
  ]

代替这个...

  array:4 [
    1649 => array:2 [
      "firstName" => "jack"
      "lastName" => "straw"
    ]
    1650 => array:2 [
      "firstName" => "jack"
      "lastName" => "straw"
    ]
    1651 => array:2 [
      "firstName" => "jack"
      "lastName" => "straw"
    ]
    1652 => array:2 [
      "firstName" => "jack"
      "lastName" => "straw"
     ]
  ]

我正在运行一个循环,可以取出重复项并找到组中最低的ID,但不确定将其折叠为一个的正确方法。

我显示的代码是搜索的结果,该搜索标识了ID,并且在这些特定字段中有重复的条目。我只是想进一步完善它而不是删除它,而是在ID 1649的末尾添加一个字段,内容为mergedWith(1650,1651,1652)

2 个答案:

答案 0 :(得分:4)

一种方法是按名字和姓氏分组,然后反向分组以获得单个ID。预先krsort输入以确保获得最低ID。

krsort($input);

//group
foreach ($input as $id => $person) {
    // overwrite the id each time, but since the input is sorted by id in descending order,
    // the last one will be the lowest id
    $names[$person['lastName']][$person['firstName']] = $id;
}

// ungroup to get the result
foreach ($names as $lastName => $firstNames) {
    foreach ($firstNames as $firstName => $id) {
        $result[$id] = ['firstName' => $firstName, 'lastName' => $lastName];
    }
}

编辑:根据您更新的问题没有太大不同。只需保留所有ID,而不是一个ID。

krsort($input);

foreach ($input as $id => $person) {
    //                   append instead of overwrite ↓ 
    $names[$person['lastName']][$person['firstName']][] = $id;
}
foreach ($names as $lastName => $firstNames) {
    foreach ($firstNames as $firstName => $ids) {
        // $ids is already in descending order based on the initial krsort
        $id = array_pop($ids);  // removes the last (lowest) id and returns it
        $result[$id] = [
            'firstName' => $firstName,
            'lastName' => $lastName,
            'merged_with' => implode(',', $ids)
        ];
    }
}

答案 1 :(得分:2)

ksort($resArr);
$tempArr = array_unique($resArr, SORT_REGULAR);
foreach ($tempArr as $key => $value) {
    foreach ($resArr as $key1 => $value2) {
        if($value['firstName'] == $value2['firstName'] && $value['lastName'] == $value2['lastName']) {
            $tempArr[$key]["mergedWith"][] = $key1;
        }
    }
}
print_r($tempArr);

$resArr = array(1650 => array(
        "firstName" => "jack",
        "lastName" => "straw"
    ),1649 => array(
        "firstName" => "jack",
        "lastName" => "straw"
    )
    ,
    1651 => array(
        "firstName" => "jack",
        "lastName" => "straw"
    ),
    1652 => array(
        "firstName" => "jack",
        "lastName" => "straw"
    ),
    1653 => array(
        "firstName" => "jack1",
        "lastName" => "straw"
    ),
    1654 => array(
        "firstName" => "jack1",
        "lastName" => "straw"
));

Output
Array
(
    [1649] => Array
        (
            [firstName] => jack
            [lastName] => straw
            [mergedWith] => Array
                (
                    [0] => 1649
                    [1] => 1650
                    [2] => 1651
                    [3] => 1652
                )

        )

    [1653] => Array
        (
            [firstName] => jack1
            [lastName] => straw
            [mergedWith] => Array
                (
                    [0] => 1653
                    [1] => 1654
                )

        )

)