2

I want to write 2 arrays in a csv file and display it horizontally.

Array 1

Array
(
    [0] => Array
        (
            [0] => 501
            [1] => 8921
        )

    [1] => Array
        (
            [0] => 502
            [1] => 8446
        )
)

Array 2

Array
(
    [0] => Array
        (
            [0] => 501
            [1] => 8900
        )

    [1] => Array
        (
            [0] => 502
            [1] => 8436
        )
)

Code to write in csv file:

header("Content-type: text/csv; charset=utf-8; encoding=utf-8");
header("Content-Disposition: attachment; filename={$exportFileName}.csv");
header("Pragma: no-cache");
header("Expires: 0");

$file = fopen('php://output', 'w');
fputcsv($file, array('HID', 'WEEK1', 'HID', 'WEEK2')); 
foreach  ($fweek as $k=>$row) 
{
    fputcsv($file, $row);  // I want to include $sweek as well             
}

But currently, it displays only the first array.

Any hint/suggestion will do a great help. Thanks in advance.

1 Answer 1

2

If they are always going to be lined up, you could use the key of the foreach to that other array and merge them. Like this:

foreach($fweek as $k => $row) {
    $row = array_merge($row, $sweek[$k]);
    fputcsv($file, $row);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are simply brilliant.Kudos....wtf I spent 2 hrs for this. Thank you so so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.