首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用php在单行显示Csv数据

使用php在单行显示Csv数据
EN

Stack Overflow用户
提问于 2022-04-26 12:25:50
回答 1查看 85关注 0票数 0

我试图使用php/wordpress导出csv,我在Which循环中有以下数组,它显示用户的“名称”

代码语言:javascript
复制
Array
(
    [0] => Meck
)

Array
(
    [0] => Bisk
)
...

与此类似,我收到的电子邮件在里面循环。

代码语言:javascript
复制
Array
(
    [0] => mailto:meck@gmail.com
)

Array
(
    [0] => mailto:bisk@gmail.com
)

现在我想在csv中导出这些数据,但是数据显示在单个列中,而不是像下面这样的单独列。

代码语言:javascript
复制
Name                        Email
meck
mailto:meck@gmail.com           
bisk
mailto:bisk@gmail.com

这是我目前的代码

代码语言:javascript
复制
global $wpdb;   
            
        $filename = 'Student_Table_' . time() . '.csv';
        $header_row = array(
            'Name',
            'Email',
        );
       $data_rows = array();
       
    
        ob_end_clean ();
        $fh = @fopen( 'php://output', 'w' );
        header( "Content-Disposition: attachment; filename={$filename}" );
        fputcsv( $fh, $header_row );

    
while ( $query->have_posts() ) : $query->the_post();
        
            $email                = get_post_meta( get_the_ID(), 'glow_user_email', true );  // contaning email of user
        
            $user_lastname              = get_post_meta( get_the_ID(), 'glow_user_lname', true );
            $user_lastname_array        = $array = explode(' ', $user_lastname);  // contaning name of user
            
            //trying to merge both arrays
            $data = $user_lastname_array.",".$email;
            fputcsv( $fh, $data );
        
        
        endwhile;
EN

回答 1

Stack Overflow用户

发布于 2022-04-26 13:22:57

将时间循环替换为以下内容:

代码语言:javascript
复制
while ( $query->have_posts() ) : $query->the_post();

    $email = get_post_meta( get_the_ID(), 'glow_user_email', true );  // contaning email of user

    $user_lastname = get_post_meta( get_the_ID(), 'glow_user_lname', true );

    $data = [trim($user_lastname[0]), trim($email[0])];
    fputcsv( $fh, $data, ',');

endwhile;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72013936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档