php filter array values and remove duplicates from multi dimensional array

前端 未结 4 1913
故里飘歌
故里飘歌 2020-12-21 12:14

Hello all im trying to find duplicate x values from this array and remove them and only leave the unique ones. For example my array is

Array
(
[0] => Arr         


        
4条回答
  •  执念已碎
    2020-12-21 12:57

    Just loop through and find unique values as you go:

    $taken = array();
    
    foreach($items as $key => $item) {
        if(!in_array($item['x'], $taken)) {
            $taken[] = $item['x'];
        } else {
            unset($items[$key]);
        }
    }
    

    Each the first time the x value is used, we save it - and subsequent usages are unset from the array.

提交回复
热议问题