How can you make a multidimensional array unique?

后端 未结 6 1228
陌清茗
陌清茗 2020-11-27 08:03

I\'ve got a multidimensional array setup like the following:

array(
  [0]=>
  array(
    [\"name\"]=> \"Foo\"
    [\"slug\"]=> \"Bar\"
  )
  [1]=>         


        
6条回答
  •  隐瞒了意图╮
    2020-11-27 08:27

    function multi_array_unique_by_value($array, $colon = '')
    {
        $ret_array = array();
        $has_array = array();
        foreach($array as $item)
        { 
            $item_array = (array)$item;
            if(!in_array($item_array[$colon], $has_array))
            {
                array_push($ret_array, $item);
                array_push($has_array, $item_array[$colon]);
            }
        }
        return $ret_array;
    }
    

    You can give your array here and give a colon name for making unique.

    On this code, you have multidimensonal array, we foreach that array, which column index for us, we pushing that column values. And when same value, its not adding return array.

    So this solution for array_unique for 1 coloumn.

提交回复
热议问题