在php中重新排列多维数组

时间:2013-09-17 17:39:48

标签: php arrays csv multidimensional-array

我有以下代码来读取csv文件

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);

输出是:

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
    )

)

我希望按照我自己的喜好按值重新排列数组(因为我将要读取的csv文件格式不同,所以我希望resultig数组完全相同[格式化]以便将来分析。 我决定使用foreach循环重新排列数组,但它没有给我我需要的结果。

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>';

print_r($ csv)返回corect结果(一个包含3个数组元素的数组),但是print_r($ n_array);返回只有一个数组的数组)

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

)

我做错了什么? 因为我不是程序员,所以对此问题的任何帮助将不胜感激。

PS:$ n_array的顺序是例如不能用作adliteram(array_reverse)

0 个答案:

没有答案