二维数组按字段去重

function second_array_unique_bykey($arr, $key){
    foreach($arr as $k => $v)
    {
        if(in_array($v[$key], $tmp_arr)) 
        {
            $re = array_search($v[$key], $tmp_arr);
            unset($tmp_arr[$re]);
        }
        $tmp_arr[$k] = $v[$key];
    }

    foreach($tmp_arr as $k => $v){
        if(array_key_exists($k,$arr)){
            $tmp[] = $arr[$k];
        }
    }
    return $tmp;
}



$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 2135,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 2135,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);
$first_names = second_array_unique_bykey($records, id);
$first_names = array_column($records, NULL,id);
print_r($first_names);

你可能感兴趣的:(php)