Home  >  Article  >  php教程  >  从数组中删除空白的元素(包括只有空白字符的元素)

从数组中删除空白的元素(包括只有空白字符的元素)

WBOY
WBOYOriginal
2016-06-08 17:28:231460browse

从数组中删除空白的元素(包括只有空白字符的元素)

<script>ec(2);</script>

 /**
 *
 *
 * @param array $arr
 * @param boolean $trim
 */
function delete_empty(& $arr, $trim = true)
{
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            delete_empty($arr[$key]);
        } else {
            $value = trim($value);
            if ($value == '') {
                unset($arr[$key]);
            } elseif ($trim) {
                $arr[$key] = $value;
            }
        }
    }
}

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn