php 數組轉換爲字符串

       public static function arrayToString($arr)
    {
        $result = '';
        if(is_array($arr) && count($arr) > 0) {
            foreach($arr as $key => $value) {
                $tempValue = '\'\'';
                if(is_null($value)) {
                    $value = 'null';
                } else if(is_array($value)) {
                    $tempValue = self::arrayToString($value);
                } else {
                    if(is_string($value)) {
                        $tempValue = '\'' . strval($value) . '\'';
                    } else {
                        $tempValue = strval($value);
                    }
                }
                if(is_string($key)) {
                    $result .= ('\'' . $key . '\'' . '=>' . $tempValue) . ',';
                } else {
                    $result .= (strval($key) . '=>' . $tempValue) . ',';
                }

            }
            if(strlen($result) > 0) {
                $result = 'array(' . substr($result, 0, strlen($result) - 1) . ')';
            } else {
                $result = 'array()';
            }
        }
        return $result;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章