幾個常用的去空、分組、調試數組函數

dump() 把數組以數組格式數組,有益於調試

function dump($vars, $label = '', $return = false){
    if (ini_get('html_errors')) {
        $content = "<pre>/n";
        if ($label != '') {
            $content .= "<strong>{$label} :</strong>/n";
        }
        $content .= htmlspecialchars(print_r($vars, true));
        $content .= "/n</pre>/n";
    } else {
        $content = $label . " :/n" . print_r($vars, true);
    }
    if ($return) { return $content; }
    echo $content;
    return null;
}

array_remove_empty()去除數組中爲空的元素

function array_remove_empty(& $arr, $trim = true){
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            array_remove_empty($arr[$key]);
        } else {
            $value = trim($value);
            if ($value == '') {
                unset($arr[$key]);
            } elseif ($trim) {
                $arr[$key] = $value;
            }
        }
    }
}

array_chunk() php默認函數 作用是把函數平均分組

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章