0

I have an array in this format:

array

That I want to output to csv. The trouble I'm having is getting that array in the right format. When I do:

foreach ($array as $row) {
   fputcsv($df, $row);
}

I only get the integer values. I am missing a column with the names like ACCESSORIES.

Here is the output:

csv

But I want column A to have the names like ACCESSORIES and column B to have the values.

3
  • an off topic question but how did you get that pretty output in first screenshot? Commented May 7, 2014 at 19:10
  • Is the actual array is looking like the same as you send in the screenshort you can use the foreach($array as $key=>$value){ } Commented May 7, 2014 at 19:27
  • Why is your input array set up like that in the first place? Why not make it an ordinary associative array, array('ACCESSORIES' => 0, 'ANNUALS' => 0, ...)? Commented May 7, 2014 at 21:34

4 Answers 4

2

fputcsv just outputs the array values; the keys would usually be column names, not additional columns in the row. You need to make an array out of each element's key and value.

foreach ($array as $row) {
    $keys = array_keys($row);
    fputcsv($df, array($keys[0], $row[$keys[0]]));
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the iteration loop of foreach like that :

foreach ($array as $key => $value) { 
       fputcsv($df, array($key[0], $value[$key[0]]));
    }

5 Comments

This won't work. When you iterate over $array, $key will be indexes 0, 1, 2, etc.
can you just display the array you want to iterate not the snap.
What do you mean by snap?
i mean to say full array that you want to iterate can you just show me that full array(13).
I can't, I'm not the OP. But It's obviously just more entries like the 6 that he showed in the screen grab.
1

if somebody is looking for exporting the results from a query with dynamic headers to an array then to csv file you could use the following function :

function exportCSVFile($fieldsNames, $result)
{

$fileName = "result.csv";
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$fileName");
header("Pragma: no-cache");
header("Expires: 0");
$stdout = fopen('php://output', 'w');

fputcsv($stdout, $fieldsNames, ',', '"'); // put the headers or fields Names

$resultArray = array();

$tempRowHolder = array();

/* 
    build the finalized array which will be used to export the result as csv 
*/

for ($i = 0; $i < count($result); $i++) {
    for ($j = 0; $j < count($fieldsNames); $j++) {
        $tempRowHolder[$j] = $result[$i][$fieldsNames[$j]]; // fetch a row by the different fields names
    }

    /* push the row from the tempRowHolder array to the main array($resultArray) */
    array_push($resultArray, $tempRowHolder);

    /* clear the temporary array (the holder of the row) to use it fresh in the next iteration */
    $tempRowHolder = [];
}

$i = 0;

/* put the finalized array into the csv file  */
while ($i < count($resultArray)) {
    fputcsv($stdout, $resultArray[$i++]);
}

fclose($stdout);
}

Comments

0

it will help you ,I used my array like :

Array
(
    [0] => Array
        (
            [NUMBER] => 67
            [TYPE] => Other
            [DATE] => 3/31/2011
        )
     [1] => Array
          (
            [NUMBER] => 87
            [TYPE] => something
            [DATE] => 3/28/2011


          )
     [2] => Array
          (
            [NUMBER] => 67
            [TYPE] => Other
            [DATE] => 3/2/2011


          )

)

<?php


$fp1 = fopen('file.csv', 'w');

foreach ($arr2 as $fields) 
{
    fputcsv($fp1, $fields);
}

fclose($fp1);

?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.