在数组中使用相同的键值合并/组合过滤的ID - 在多维中是唯一的 - PHP

时间:2016-05-11 11:57:40

标签: php arrays multidimensional-array merge unique

我有一个数组的多维结果,其中包含多个数组对象,需要在数组中应用唯一ID进行复制,并将其他过滤后的id与数组相同的键值对合并。如下所示结果

这需要服务。

结果集

Array
(
    [0] => Array
        (
            [id] => 24901
            [course_title_code] => VTM 001
            [instructors_name] => Master_Al_Za
            [course_code] => 8734
            [unique_id] => VTM_001_8734_Master_Al_Za
        )

    [1] => Array
        (
            [id] => 25669
            [course_title_code] => VTM 002
            [instructors_name] => Ali_Al
            [course_code] => 8732
            [unique_id] => VTM_002_8732_Ali_Al
        )

    [2] => Array
        (
            [id] => 26011
            [course_title_code] => VTM 002
            [instructors_name] => Ali_Al
            [course_code] => 8732
            [unique_id] => VTM_002_8732_Ali_Al
        )

    [3] => Array
        (
            [id] => 29502
            [course_title_code] => VTM 001
            [instructors_name] => Master_Al_Za
            [course_code] => 8734
            [unique_id] => VTM_001_8734_Master_Al_Za
        )

    [4] => Array
        (
            [id] => 49202
            [course_title_code] => VTM 001
            [instructors_name] => Master_Al_Za
            [course_code] => 8734
            [unique_id] => VTM_001_8734_Master_Al_Za
        )    

)

期望的结果

Array
(
    [0] => Array
        (
            [id] => 24901,29502,49202
            [course_title_code] => VTM 001
            [instructors_name] => Master_Al_Za
            [course_code] => 8734
            [unique_id] => VTM_001_8734_Master_Al_Za
        )

    [1] => Array
        (
            [id] => 25669,26011
            [course_title_code] => VTM 002
            [instructors_name] => Ali_Al
            [course_code] => 8732
            [unique_id] => VTM_002_8732_Ali_Al
        )
)

我目前的实施尝试做这样的事情:

$data = array_unique_by_key($data, 'unique_id');

function array_unique_by_key (&$array, $key) {
    $tmp = array();
    $result = array();
    foreach ($array as $p => $value) {
        if (!in_array($value[$key], $tmp)) {
            array_push($tmp, $value[$key]);
            array_push($result, $value);
        }else{
            $result[$p] = $value['id'];
        }
    }
    return $array = $result;
}

3 个答案:

答案 0 :(得分:1)

你几乎就在那里,只需要根据唯一值

在新数组中追加值

如果key已经存在,则追加id,追加整个数组

并在需要时使用array_values将密钥更新为数字

$x = array(
    0 => array
        (
            'id' => '24901',
            'course_title_code' => 'VTM 001',
            'instructors_name' => 'Master_Al_Za',
            'course_code' => '8734',
            'unique_id' => 'VTM_001_8734_Master_Al_Za',
        ),

    1 => array
        (
            'id' => '25669',
            'course_title_code' => 'VTM 002',
            'instructors_name' => 'Ali_Al',
            'course_code' => '8732',
            'unique_id' => 'VTM_002_8732_Ali_Al',
        ),

    2 => array
        (
            'id' => '26011',
            'course_title_code' => 'VTM 002',
            'instructors_name' => 'Ali_Al',
            'course_code' => '8732',
            'unique_id' => 'VTM_002_8732_Ali_Al',
        ),

    3 => array
        (
            'id' => '29502',
            'course_title_code' => 'VTM 001',
            'instructors_name' => 'Master_Al_Za',
            'course_code' => '8734',
            'unique_id' => 'VTM_001_8734_Master_Al_Za',
        ),

    4 => array
        (
            'id' => '49202',
            'course_title_code' => 'VTM 001',
            'instructors_name' => 'Master_Al_Za',
            'course_code' => '8734',
            'unique_id' => 'VTM_001_8734_Master_Al_Za',
        )    

);
$data = array_unique_by_key($x, 'unique_id');

function array_unique_by_key (&$array, $key) {
    $result = array();
    foreach ($array as $p => $value) {
        if (!array_key_exists($value[$key], $result)) {
            $result[$value[$key]] = $value;
        }else{
            $result[$value[$key]]['id'] .= ',' . $value['id'];
        }
    }
    return array_values($result);
}

print_r($data);

输出:

Array
(
    [0] => Array
        (
            [id] => 24901,29502,49202
            [course_title_code] => VTM 001
            [instructors_name] => Master_Al_Za
            [course_code] => 8734
            [unique_id] => VTM_001_8734_Master_Al_Za
        )

    [1] => Array
        (
            [id] => 25669,26011
            [course_title_code] => VTM 002
            [instructors_name] => Ali_Al
            [course_code] => 8732
            [unique_id] => VTM_002_8732_Ali_Al
        )

)

答案 1 :(得分:1)

生成所需的精确输出。使用逗号连接重复值:

function mergeArrayOnKey($array, $key){
    $keychain = $ret = array();
    foreach($array as $a){
        if(!isset($a[$key])) continue;
        if(!in_array($a[$key], $keychain)){
            array_push($keychain, $a[$key]);
            array_push($ret, $a);
        }else{
            // Get sub array
            foreach($ret as $k=>$r){
                if($r[$key] == $a[$key]){
                    foreach($a as $kk=>$vv){
                        $oldvals = explode(",",$r[$kk]);
                        if(in_array($vv, $oldvals)) continue;
                        array_push($oldvals,$vv);
                        $r[$kk] = implode(",",$oldvals);
                    }
                }
                $ret[$k] = $r;
            }
        }
    }
    return $ret;
}

这是一个小提琴:https://3v4l.org/4DNgt

输出:

Array
(
    [0] => Array
        (
            [id] => 24901,29502,49202
            [course_title_code] => VTM 001
            [instructors_name] => Master_Al_Za
            [course_code] => 8734
            [unique_id] => VTM_001_8734_Master_Al_Za
        )

    [1] => Array
        (
            [id] => 25669,26011
            [course_title_code] => VTM 002
            [instructors_name] => Ali_Al
            [course_code] => 8732
            [unique_id] => VTM_002_8732_Ali_Al
        )

)

答案 2 :(得分:0)

这可能是解决方案。我只测试3个值,请检查并告诉我。有一些偏移警告,忽略它们。

Online Check,您可以在线查看。

<强>阵列:

$arr = array(
    array(
        "id" => 24901,
        "course_title_code" => "VTM 001",
        "instructors_name" => "Master_Al_Za",
        "course_code" => 8734,
        "unique_id" => "VTM_001_8734_Master_Al_Za"
    ),
    array(
        "id" => 25669,
        "course_title_code" => "VTM 002",
        "instructors_name" => "Ali_Al",
        "course_code" => 8732,
        "unique_id" => "VTM_002_8732_Ali_Al"
    ),
    array(
        "id" => 26011,
        "course_title_code" => "VTM 002",
        "instructors_name" => "Ali_Al",
        "course_code" => 8732,
        "unique_id" => "VTM_002_8732_Ali_Al"
    )
);

<强>的作用机制:

$out = array();
$uni_key = array();
foreach($arr as $key => $val){
    $srch = array_search($val['unique_id'], $uni_key);
    if($srch > -1){
        $out[$srch]['id'] .= $out[$key]['id'].", ".$val['id'];
    }else{
        array_push($uni_key, $val['unique_id']);
        $out[$key] = $val;      
    }       
}

echo '<pre>';
print_r($out);

<强>结果:

Array
(
    [0] => Array
        (
            [id] => 24901
            [course_title_code] => VTM 001
            [instructors_name] => Master_Al_Za
            [course_code] => 8734
            [unique_id] => VTM_001_8734_Master_Al_Za
        )

    [1] => Array
        (
            [id] => 25669, 26011
            [course_title_code] => VTM 002
            [instructors_name] => Ali_Al
            [course_code] => 8732
            [unique_id] => VTM_002_8732_Ali_Al
        )

)