How to get wordpress records and export them in a .csv with php?

0

I have been trying to get user information from a wordpress database and export it in a .csv file

This is the code I'm using:

<section>
    <div>
            <?php
            $array_export_cvs = array();
            $array_export_cvs[] = array("Correo", "Nombre", "Cedula", "Departamento", "Ciudad", "Fecha de nacimiento");
            $args = array('role' => 'subscriber');
            $current_user = get_users( $args );
                foreach($current_user as $user){
                    $full_name = get_user_meta($user->ID, 'pie_text_4', true) .' '. get_user_meta($user->ID, 'pie_text_3', true);
            $cedula = get_user_meta($user->ID, 'pie_text_36', true);
            $departamento = get_user_meta($user->ID, 'pie_text_11', true);
            $ciudad = get_user_meta($user->ID, 'pie_text_12', true);
                    $fecha = get_user_meta($user->ID, 'pie_date_9', true);
                    $fecha = $fecha[date]; 
                    $array_export_cvs[] = array($user->user_email, $full_name, $cedula, $departamento, $ciudad, $fecha);
                    echo(json_encode($array_export_cvs));
                }           
            $fp = fopen('fichero.csv', 'w');
            foreach ( $array_export_cvs as $line ) {
                fputcsv($fp, $line,';');
            }
            fclose($fp);
            ?>
        <a class="btn btn-default" href="/fichero.csv" download="fichero.csv">Download</a>
    </div>
</section>

So far, the code works well for me and generates the following array:

["[email protected]","Usuario Prueba","145127843","Departamento2","Ciudad3",["14\/10\/1991"]]

However, when viewing the generated .csv file, I find the following:

[email protected];"Usuario Prueba";145127843;Departamento2;Ciudad3;Array

The date of birth is generating some kind of error and the word "Array" simply appears in the generated file.

Does anyone have knowledge that could be failing? Thank you very much to those who can help me.

    
asked by JayCorts 21.11.2018 в 05:32
source

0 answers