How to remove duplicate values from an array in PHP

后端 未结 24 2048
故里飘歌
故里飘歌 2020-11-22 03:40

How can I remove duplicate values from an array in PHP?

24条回答
  •  佛祖请我去吃肉
    2020-11-22 04:00

    An alternative for array_unique() function..

    Using Brute force algorithm

    //[1] This our array with duplicated items

    $matches = ["jorge","melvin","chelsy","melvin","jorge","smith"];

    //[2] Container for the new array without any duplicated items

    $arr = [];

    //[3] get the length of the duplicated array and set it to the var len to be use for for loop

    $len = count($matches);

    //[4] If matches array key($i) current loop Iteration is not available in //[4] the array $arr then push the current iteration key value of the matches[$i] //[4] to the array arr.

    for($i=0;$i if(array_search($matches[$i], $arr) === false){ array_push($arr,$matches[$i]); } } //print the array $arr. print_r($arr); //Result: Array ( [0] => jorge [1] => melvin [2] => chelsy [3] => smith )

提交回复
热议问题