Search code examples
phparraysexport-to-csvarray-flip

Flip values in array to display them in column instead of row via csv


<?php
// output headers so that the file is downloaded rather than displayed
	header('Content-type: text/csv');
	header('Content-Disposition: attachment; filename="csv.csv"');
 
// do not cache the file
	header('Pragma: no-cache');
	header('Expires: 0');
 
// create a file pointer connected to the output stream
	$file = fopen('php://output', 'w');
 
// send the column headers
	fputcsv($file, array('Filepath', 'Folders', 'Date'));
	
// data
 	
 	$shopurl = "https://www.example.com/";
	
	$scanner = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
	
	$Fpath = array(); 
	$Folders = array(); 
	$Date = array(); 
	
		foreach ($scanner as $fileInfo) {
		
		    if ($fileInfo->isDir()){ 
		        continue;
		    }
			
			$filepath = substr($fileInfo->getPathname(), 2);
			$cur_dir = explode('/', getcwd());
			
		    $Fpath[] = $shopurl . $filepath;
		    $Folders[] = substr(pathinfo($fileInfo->getPathname(), PATHINFO_DIRNAME), 2);
		    $Date[] = date ("F d Y H:i:s.", filemtime($fileInfo));
		
		}

	$data = array($Fpath, $Folders, $Date);
	
 
// output each row of the data
	foreach ($data as $column)
	{
		fputcsv($file, $column);
	}
 
	exit();

With the code above I tried to get a csv with three columns. The output of the arrays look like:

Array
(
    [0] => Array
        (
            [0] => https://www.example.com/GHI/123/new.php
            [1] => https://www.example.com/ABC/123.php
            [2] => https://www.example.com/output.php
            [3] => https://www.example.com/csv.php
        )

    [1] => Array
        (
            [0] => GHI/123
            [1] => ABC
            [2] => 
            [3] => 
        )

    [2] => Array
        (
            [0] => April 12 2018 11:15:03.
            [1] => April 13 2018 10:28:30.
            [2] => April 13 2018 12:36:16.
            [3] => April 13 2018 12:32:10.
        )

)

So the problem right now is that all filepaths are displayed in the first array / row, the folder names are in second and date in last. To get the arrays correctly displayed in csv I need to have for each array / row the three values (filepath, folder, date).

I tried to get it done with array_flip but it doesn't work. Maybe someone has an advice. Thanks!


Solution

  • The problem should be solvable by changing how you load the values in your first code-snippet. If you want the structure to be something like

    [0] => Array
            (
                ['fpath'] => https://www.example.com/GHI/123/new.php
                ['folder'] => GHI/123
                ['date'] => April 12 2018 11:15:03.
            )
    [1] => Array
            (
                ['fpath'] => https://www.example.com/GHI/123/new.php
                ['folder'] => GHI/123
                ['date'] => April 12 2018 11:15:03.
            )
    

    Then the following should work for you:

    <?php
    // output headers so that the file is downloaded rather than displayed
        header('Content-type: text/csv');
        header('Content-Disposition: attachment; filename="csv.csv"');
    
    // do not cache the file
        header('Pragma: no-cache');
        header('Expires: 0');
    
    // create a file pointer connected to the output stream
        $file = fopen('php://output', 'w');
    
    // send the column headers
        fputcsv($file, array('Filepath', 'Folders', 'Date'));
    
    // data
        $scanner = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
        $data = array();
    
        foreach ($scanner as $fileInfo) {
            $data[] = array(
                        "fpath" => "https://www.example.com/" . substr($fileInfo->getPathname(), 2),
                        "folder" => substr(pathinfo($fileInfo->getPathname(), PATHINFO_DIRNAME), 2),
                        "date" => date ("F d Y H:i:s.", filemtime($fileInfo))
                    );
        }
    
    // output each row of the data
        foreach ($data as $column)
        {
            fputcsv($file, $column);
        }
    
        exit();