doubi8383 2013-09-17 17:39
浏览 15

在php中重新排列多维数组

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)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大