Recursively remove empty elements from an associative array in PHP

While there are other built-in functions to remove empty array values like array_filter with callback etc for associative arrays it is important to have similar methods when dealing with a large number of data sets.

Following is an example of a recursive function to do that. The function takes the array and recursively call itself if the array is an associative array. You can change the empty condition to remove values other than empty values from the array too.

 /**
  * Remove any elements where the value is empty
  * @param  array $array the array to walk
  * @return array
  */
  function removeEmptyValues(array &$array){
    foreach ($array as $key => &$value) {
      if (is_array($value)) {
        $value = removeEmptyValues($value);
      }
      if (empty($value)) {
        unset($array[$key]);
      }
    }
    return $array;
  }
}