The php array removes duplicate values based on the key name!

there is an array of user_ realname values in the figure. Zhu Fei has duplicates. Now if you want to remove the duplicates, just keep one. How to write this cycle? I am a rookie. I hope my predecessors can give me some advice. no, no, no.

Aug.05,2021

you can try this opportunistic game

function repeat(array $input, string $key): array
    {
        $data = [];
        
        foreach ($input as $val) {
            $data[$val[$key]] = $val;
        }
        
        sort ($data);
        
        return $data;
    }
    
    
    var_dump (repeat ($data, 'user_realname'));

if it is found in the database, use group by user_realname when querying.
if the data is written dead, compare it with the foreach loop and remove it repeatedly with unset ().


if the data is small, it is assumed that the first

is retained.
<?php

$arr = [
    [
        'id_user'=>1,
        'user_realname'=>''
    ],
    [
        'id_user'=>2,
        'user_realname'=>''
    ],
    [
        'id_user'=>3,
        'user_realname'=>''
    ],
    [
        'id_user'=>4,
        'user_realname'=>''
    ],
    [
        'id_user'=>5,
        'user_realname'=>''
    ],
];

$isExistRealName = [];
$newArr = array_filter($arr, function ($v) use (&$isExistRealName) {
    if (!in_array($v['user_realname'], $isExistRealName)) {
        array_push($isExistRealName, $v['user_realname']);
        return true;
    }
    
    return false;
});

var_dump($newArr);

array_unqiue

Menu