Remove Array Nulls

Posted by Mr PHP on

Recursively removes array keys with null values.

<?php

function remove_array_nulls($data)
{
  foreach ($data as $key=>$value)
  {
    if (is_array($data[$key]))
    {
      $output[$key] = remove_array_nulls($value);
    }
    elseif ($data[$key] || $data[$key]===0)
    {
      $output[$key] = $value;
    }
  }
  return $output;
}

Tagged with : PHP


Comments