I have the folowing code for reading a csv file
function read_CSV_royal($royal_csv) {
$file_handle = fopen($royal_csv, 'r');
while (!feof($file_handle)) {
$line_of_text[] = fgetcsv($file_handle, 1024, ';');
}
fclose($file_handle);
return $line_of_text;
}
$royal_csv = 'royal.csv';
$csv1 = read_CSV_royal($royal_csv);
the output is :
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] => 12
[6] => curr3
[7] => in stoc
)
)
i want to rearange the array by values after my own liking ( because the csv files that i will be reading are not formated the same, so i would like that the resultig arrays to be exactly alike [formating wise] for future analisys. I decided to use a foreach loop to rearange the array but it does not give me the results i need.
function read_CSV_royal($royal_csv) {
$file_handle = fopen($royal_csv, 'r');
while (!feof($file_handle)) {
$line_of_text[] = fgetcsv($file_handle, 1024, ';');
}
fclose($file_handle);
return $line_of_text;
}
$royal_csv = 'royal.csv';
$csv1 = read_CSV_royal($royal_csv);
foreach ($csv1 as $key => &$value) {
$n_array = array($value[7],$value[6],$value[5],$value[4],$value[3],$value[2],$value[1],$value[0]);
}
echo '<pre>';
print_r ($csv1);
echo '</pre>';
echo '<pre>';
print_r ($n_array);
echo '</pre>';
the print_r($csv) returns the corect result ( an array with 3 array elements), but the print_r ($n_array); returns an array with only one array)
Array
(
[0] => Array
(
[0] => in stoc
[1] => curr3
[2] => 12
[3] => editor3
[4] => code3
[5] => category3
[6] => description3
[7] => book3
)
)
What am i doing wrong? As i am not a programmer any help with this issue would be greatly appreciated.
PS : the order of $n_array is for exemple not to be used as adliteram (array_reverse)