Hackerss.com

Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in
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

Top comments (0)