PHP二维数组中相同的一列值去重只保留一项

分类首页日期3个月前访问150评论0
  $inputArray = [
            [
                "id" => 44,
                "line_show_code" => "3",
                "line_name" => "三北线",
                "line_color" => "e89e47ff",
                "station_name_en" => "yantang",
                "station_name" => "燕塘"
            ],
            [
                "id" => 150,
                "line_show_code" => "gf",
                "line_name" => "广佛线",
                "line_color" => "bbd80aff",
                "station_name_en" => "yangang",
                "station_name" => "燕岗"
            ],
            [
                "id" => 199,
                "line_show_code" => "6",
                "line_name" => "六号线",
                "line_color" => "6e0346ff",
                "station_name_en" => "yantang",
                "station_name" => "燕塘"
            ]
        ];
 
        $uniqueStations = [];
 
        foreach ($inputArray as $item) {
            $stationName = $item["station_name"];
            if (!isset($uniqueStations[$stationName])) {
                $uniqueStations[$stationName] = $item;
            }
        }
 
        $filteredArray = array_values($uniqueStations);
 
        print_r($filteredArray);
 
 
    结果:
 
 array:2 [
  0 => array:6 [
    "id" => 44
    "line_show_code" => "3"
    "line_name" => "三北线"
    "line_color" => "e89e47ff"
    "station_name_en" => "yantang"
    "station_name" => "燕塘"
  ]
  1 => array:6 [
    "id" => 150
    "line_show_code" => "gf"
    "line_name" => "广佛线"
    "line_color" => "bbd80aff"
    "station_name_en" => "yangang"
    "station_name" => "燕岗"
  ]
]