Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have the folowing array read from a csv file:

Array
(
[0] => Array
    (
        [0] => book1
        [1] => description1
        [2] => category1
        [3] => code1
        [4] => editor1
        [5] => 0
        [6] => eur
        [7] => out of stoc
    )

[1] => Array
    (
        [0] => book2
        [1] => description2
        [2] => category2
        [3] => code2
        [4] => editor2
        [5] => 0
        [6] => curr2
        [7] => out of stoc
    )

[2] => Array
    (
        [0] => book3
        [1] => description3
        [2] => category3
        [3] => code3
        [4] => editor3
        [5] => 0
        [6] => curr3
        [7] => out of stoc
    )

)

Code :

function read_CSV_raza($rzs_csv) {
$file_handle = fopen($rzs_csv, 'r');
while (!feof($file_handle)) {
$line_of_text[] = fgetcsv($file_handle, 1024, ';');
}
fclose($file_handle);
return $line_of_text;
}

$rzs_csv = 'rzs.csv';
$csv = read_CSV_raza($rzs_csv);

I want to add another array (headers) in order to write this intro another csv file. EX :

Array
(
[0] => Array
(
    [0] => h1
    [1] => h2
    [2] => h3
    [3] => h4
    [4] => h5
    [5] => h6
    [6] => h7
    [7] => h8
)
[1] => Array
(
    [0] => book1
    [1] => description1
    [2] => category1
    [3] => code1
    [4] => editor1
    [5] => 0
    [6] => eur
    [7] => out of stoc
)

[2] => Array
(
    [0] => book2
    [1] => description2
    [2] => category2
    [3] => code2
    [4] => editor2
    [5] => 0
    [6] => curr2
    [7] => out of stoc
)

[3] => Array
(
    [0] => book3
    [1] => description3
    [2] => category3
    [3] => code3
    [4] => editor3
    [5] => 0
    [6] => curr3
    [7] => out of stoc
)

)

I tried with array_merge(); array_unshift(); but to no avail, i do not get the results i need.

The cod for writing the array in the new CSV is :

$fp = fopen('rezultate.csv', 'w') or die("Can't open rezultate.csv");
foreach ($csv as $items) {
fputcsv($fp, $items, ';', '"');
}
fclose($fp) or die("Can't close rezultate.csv");

As i am not a programmer any help with this issue would be greatly appreciated.

share|improve this question
3  
Just do an fputcsv($fp, $headers, ';', '"'); immediately before your foreach ($csv as $items) loop.... it's a lot more efficient than array_unshift() –  Mark Baker Sep 17 '13 at 16:45
    
just did but now it only writes the headers not the array –  Rasvan Sep 17 '13 at 16:50
    
$fp = fopen('rezultate.csv', 'w') or die("Can't open rezultate.csv"); fputcsv($fp, $headers, ';', '"'); foreach ($csv as $items) { fputcsv($fp, $items, ';', '"'); } fclose($fp) or die("Can't close rezultate.csv"); –  Mark Baker Sep 17 '13 at 16:50
    
thank you so much works like a dream! –  Rasvan Sep 17 '13 at 16:54
add comment

1 Answer

array_unshift worked for me:

$add = array (
    0 => "h1",
    1 => "h2",
    2 => "h3",
    3 => "h4",
    4 => "h5",
    5 => "h6",
    6 => "h7",
    7 => "h8"
);
array_unshift($myarray, $add);

And also

array_unshift($myarray, array(0 => "h1", 1 => "h2", 2 => "h3", 3 => "h4", 4 => "h5", 5 => "h6", 6 => "h7", 7 => "h8"));
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.