• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    使用lua实现php的print_r()函数功能
    POST TIME:2021-10-18 16:55

    之前写了一些类似php的函数,下面再来一个print_r()函数,代码如下:

    复制代码 代码如下:

    function pr (t, name, indent)  
        local tableList = {}  
        function table_r (t, name, indent, full)  
            local id = not full and name or type(name)~="number" and tostring(name) or '['..name..']'  
            local tag = indent .. id .. ' = '  
            local out = {}  -- result  
            if type(t) == "table" then  
                if tableList[t] ~= nil then  
                    table.insert(out, tag .. '{} -- ' .. tableList[t] .. ' (self reference)')  
                else 
                    tableList[t]= full and (full .. '.' .. id) or id 
                    if next(t) then -- Table not empty  
                        table.insert(out, tag .. '{')  
                        for key,value in pairs(t) do  
                            table.insert(out,table_r(value,key,indent .. '|  ',tableList[t]))  
                        end  
                        table.insert(out,indent .. '}')  
                    else table.insert(out,tag .. '{}') end  
                end  
            else 
                local val = type(t)~="number" and type(t)~="boolean" and '"'..tostring(t)..'"' or tostring(t)  
                table.insert(out, tag .. val)  
            end  
            return table.concat(out, '\n')  
        end  
        return table_r(t,name or 'Value',indent or '')  
    end  
    function print_r (t, name)  
        print(pr(t,name))  
    end  
     
    local a = {x=1, y=2, label={text='hans', color='blue'}, list={'a','b','c'}}  
     
    print_r(a) 

    您可能感兴趣的文章:
    • php中理解print EOT分界符和echo EOT的用法区别小结
    • PHP学习之输出字符串(echo,print,printf,print_r和var_dump)
    • 用js写了一个类似php的print_r输出换行功能
    • php输出echo、print、print_r、printf、sprintf、var_dump的区别比较
    • PHP echo,print,printf,sprintf函数之间的区别与用法详解
    • 用nodejs实现PHP的print_r函数代码
    • PHP中echo,print_r与var_dump区别分析
    • php中print(),print_r(),echo()的区别详解
    上一篇:使用lua实现split字符串分隔
    下一篇:使用lua实现php的var_dump()函数功能
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信