Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a CSV file from a PHP Array

Tags:

php

csv

fopen

I appreciate there are already a lot of questions around this subject. But, as I'm not a PHP developer, I'm struggling to get anything to work for my specific object structure. I have a pretty flat JSON structure that is being read into PHP just fine. All I need to do is go through the array of objects, create the CSV headings from the keys in the first object (they are all the same), and then write each line of the CSV using all the object properties. Here's a look at my PHP data:

Array
(
    [0] => stdClass Object
        (
            [record id] => -KA9S-beTbe9LIPBm_xK
            [timestamp] => Wed Feb 10 2016 10:12:20 GMT+0100
            [user id] => 33037t23nxyx1x5k
            [user age] => 18-24
            [user gender] => M
            [user q1] => Yes
            [user q2] => Yes
            [user q3] => more
            [answer q1 value] => 1
            [answer q1 confidence] => 3
            [answer q1 duration] => 262412
            [answer q2 value] => 1
            [answer q2 confidence] => 3
            [answer q2 duration] => 1959
            [answer q3 value] => 0
            [answer q3 confidence] => 3
            [answer q3 duration] => 2939
            [answer q4 value] => 1
            [answer q4 confidence] => 2
            [answer q4 duration] => 1868
            [answer q5 value] => 1
            [answer q5 confidence] => 2
            [answer q5 duration] => 2196
        )

So, you can see it should be pretty straightforward. The keys would be the CSV cell headings and then a row for each record.

I hope someone can help me. I don't know PHP at all.

UPDATE: Here's my latest PHP file. This works. I had to cast the Object to an Array to get the Keys:

<?php

    $data = $_POST['data'];
    $response = json_decode($data);

    $records_arr = $response->records;

    $fp = fopen('records.csv', 'w');
    $i = 0;

    foreach ($records_arr as $record) {


        // $record is an object so create an array
        $record_arr = array();

        foreach ($record as $value) {
            $record_arr[] = $value;
        }

        if($i == 0){
            fputcsv($fp, array_keys((array)$record));
        }
        fputcsv($fp, array_values($record_arr));
        $i++;
    }

    fclose($fp);

    echo print_r($records_arr);
?>

Thank you everyone for the help. Amazing community!

like image 275
Lee Probert Avatar asked Dec 19 '22 19:12

Lee Probert


1 Answers

For writing a PHP array to a CSV file you should use the built in PHP function fputcsv:

<?php

$list = array (
    array('header 1', 'header 2', 'header 3', 'header 4'),
    array('5656', '454545', '5455', '5454'),
    array('541212', '454545', '5455', '5454'),
    array('541212', '454545', '5455', '5454'),
);

$fp = fopen('file.csv', 'wb');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);

Edit:

More specific example based on your data input:

<?php

// I had to recreate your data, this is just an example
$array_of_objects = array();
for( $i = 0; $i < 5; $i++ )
{
    $obj = new \stdClass();
    $obj->username = "Rein";
    $obj->answer = "Correct";
    $obj->awesomeness = 1000;

    $array_of_objects[] = $obj;
}

// Open a file to write to
$fp = fopen('file.csv', 'wb');

$i = 0;

// Loop the array of objects
foreach( $array_of_objects as $obj )
{
    // Transform the current object to an array
    $fields = array();

    foreach ($obj as $k => $v)
    {
        $fields[ $k ] = $v;
    }

    if( $i === 0 )
    {
        fputcsv($fp, array_keys($fields) ); // First write the headers
    }

    fputcsv($fp, $fields); // Then write the fields

    $i++;
}

fclose($fp);

Edit 2:

Based on new information (you have a JSON string to start with), I'm added this example as a better way to write the data to a CSV file:

<?php

$data = json_decode($_POST['data'], TRUE);
$records = $data['records'];

$fp = fopen('records.csv', 'wb');

$i = 0;
foreach ($records as $record) {

    if($i === 0) {
        fputcsv($fp, array_keys($record));
    }

    fputcsv($fp, array_values($record));
    $i++;
}

fclose($fp);
like image 99
Rein Avatar answered Dec 31 '22 21:12

Rein