dpjhq00684 2010-07-14 18:00
浏览 60
已采纳

将数组转换为PHP代码

Following my last post about better syntax for arrays, I've decided to use JSON format for arrays and use a method to convert them into PHP code.

Note that the ultimate goal of this is to be write a $config array in JSON and translate that into PHP code (so I can avoid having to use PHP's ugly array syntax):

The function works fine for arrays of arbitrary size and dimension, maybe I could improve it by having it automatically indent, but there isn't much more. Does anyone here any suggestions on how to make it better?

  • 写回答

5条回答 默认 最新

  • douxuanwei1980 2010-07-14 19:37
    关注

    OK, after hearing all the feedback from everybody here, I've decided to make a "compromise." My main beef with the existing array syntax is its bad readability, which can certainly be improved (a lot) by using indentation.

    Because I am lazy to indent (and the files I am writing are huge), I opted for JSON (or any syntax that's more readable than PHP's). I didn't make myself very clear, but another strong reason why I am using the JSON format because many other people will be looking at these config files. Most of them are not PHP-savvy and JSON is a much more human-readable format.

    Unfortunately PHP code formatters/beautifiers out there don't do anything to array formatting, so I coded my own. It is based on that ugly piece of code I wrote above (and it is uglier), but it does the job.

    The result is now I basically have an array beautifier, and I can generate readable native PHP code while being lazy. That's all I wanted, thanks everybody for the suggestions and pointers.

    PS: Here's the beautified Kohana config array I generated with my function:

    array (
        'default'   => array (
            'type'         => 'mysql',
            'connection'   => array (
                'hostname'   => 'localhost',
                'database'   => 'kohana',
                'username'   => false,
                'password'   => false,
                'persistent' => false
            ),
            'table_prefix' => '',
            'charset'      => 'utf8',
            'caching'      => false,
            'profiling'    => true
        ),
        'alternate' => array (
            'type'         => 'pdo',
            'connection'   => array (
                'dsn'        => 'mysql:host=localhost;dbname=kohana',
                'username'   => 'root',
                'password'   => 'r00tdb',
                'persistent' => false
            ),
            'table_prefix' => '',
            'charset'      => 'utf8',
            'caching'      => false,
            'profiling'    => true
        )
    );
    

    Which doesn't look bad at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)