Hackerss.com

hackerss
hackerss

Posted on

Php Remove duplicates from array and also remove all nulls with comments


<?php

/**
 * function to remove duplicates from array and also remove all nulls with comments with comments
 * @param array $array  Array to remove duplicates from
 * @return $array
 */ 
function removeDuplicates($array) {
  // remove duplicates from array
  $array = array_unique($array);
  // remove all nulls from array
  $array = array_filter($array, function($value) { return $value !== null; });
  // return the array
  return $array;
}

//example
$result = removeDuplicates(['a', 'b', 'c', 'a', 'b', 'c', null]);

//output
echo $result; //['a', 'b', 'c']
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)