dongqie7806 2016-07-22 10:49
浏览 36
已采纳

从表中显示列名称为pascal case

I have created a table in phpmyadmin with column names like first_name, last_name. When I use the command to show column names from the table, it displays them as first_name.

I want to display my column names like First Name. Can you please tell me how to show column names in pascal case.

  • 写回答

1条回答 默认 最新

  • doudaochu1699 2016-07-22 11:00
    关注

    This code converts some_text to Some Text:

    $before = array('first_name', 'last_name', 'something');
    
    $after = array();
    foreach($before as $v){
        $after[] = ucwords(implode(' ', explode('_', $v)));
    }
    

    Result:

    Array
    (
        [0] => First Name
        [1] => Last Name
        [2] => Something
    )
    

    And another way of doing the same:

    // .. foreach ..
    $after[] = implode(' ', array_map('ucfirst', explode('_', $v)));
    // code
    

    The output is the same as before.



    The following example shows how to do it when running SHOW COLUMNS FROM TABLENAME. I used PDO. Take a look:

    $con = new PDO('con string here', 'DB_USERNAME', 'DB_PASSWORD');
    $stmt = $con->prepare('SHOW COLUMNS FROM tablename');
    $stmt->execute();
    $columns = array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $columns[] = array(
            'original' => $row['Field'],
            'pascal' => ucwords(implode(' ', explode('_', $row['Field'])))
        );
    }
    

    The result of $columns will be something like this:

    Array
    (
        [0] => Array
            (
                [original] => first_name
                [pascal] => First Name
            )
    
        [1] => Array
            (
                [original] => last_name
                [pascal] => Last Name
            )
    
        [2] => Array
            (
                [original] => other_column
                [pascal] => Other Column
            )
    
        [3] => Array
            (
                [original] => something
                [pascal] => Something
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突