本文介绍了交换二维数组的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将 CSV 文件中的列放入数组(多维数组或数组数组)中.我使用了 CSV.au3 加载 rows 就好了,但我需要 columns 在那个地方.我的 CSV 文件如下所示:
I need to get columns from a CSV file into an array (multidimensional array or array of arrays). I used CSV.au3 which loads the rows just fine, but I need the columns in that place. My CSV file looks like:
Item 1, Item 2, Another Item
Item 3, Item 4
它创建了一个多维数组,如下所示:
It creates a multidimensional array that looks like:
$aResult[0] = [0] => 'Item 1', [1] => 'Item 2', [2] => 'Another Item'
$aResult[1] = [0] => 'Item 3', [1] => 'Item 4'
我希望它看起来像什么:
Where I would like it to look like:
$aResult[0] = [0] => 'Item 1', [1] => 'Item 3'
$aResult[1] = [0] => 'Item 2', [1] => 'Item 4'
$aResult[2] = [0] => 'Another Item'
对于每一行,它应该包含列而不是行.
For each row it should contain the column not the row.
推荐答案
我从这个项目中休息了一段时间,最终自己解决了这个问题:
I had a break from this project for a while, and ended up figuring it out myself:
;Load the first line into an array (for our Count Later)
$columnsCounter = StringSplit($content[1], ",")
;Here we use the row count (from $content) and column count (from $columnsCounter)
;We define an array that is the perfect size for our Menu Items
Dim $MenuItems[$content[0] + 1][$columnsCounter[0] + 1]
; Now we loop through each row and column
For $x = 1 To ($content[0]) - 1
;Create an array with the row we are looking at
$oneRow = StringSplit($content[$x], ",")
;now loop through each column
For $y = 1 To ($columnsCounter[0])
;Grab the item we want and add it to our menu items array
$MenuItems[$x][$y] = $oneRow[$y]
Next
Next
这篇关于交换二维数组的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!