ubi*_*con 3 php csv arrays coldfusion multidimensional-array
我正在尝试使用ColdFusion(版本7.0.2.142559)将CSV文件加载到数组中.现在我收到以下错误:
coldfusion.runtime.Struct类型的标量值无法分配给二维ColdFusion数组.ColdFusion二维数组只能容纳一维ColdFusion数组和Java List对象.
我的CSV文件是以这种格式设置的:
a,b
c,d
e,f
Run Code Online (Sandbox Code Playgroud)
这是我第一次使用ColdFusion,所以我可能有一些我看不到的简单语法错误.代码如下.
<!--- get the current full path of the current --->
<cfset currentPath = getCurrentTemplatePath()>
<cfset currentDirectory = getDirectoryFromPath(currentPath)>
<!--- get and read the CSV-TXT file --->
<cffile action="read" file="#currentDirectory#/smalltest.csv" variable="csvfile">
<!--- create a new array --->
<cfset array=ArrayNew(2)>
<!--- loop through the CSV-TXT file on line breaks and insert into database --->
<cfloop index="index" list="#csvfile#" delimiters="#chr(10)##chr(13)#">
<cfset array[#index#][1]=#listgetAt('#index#',1, ',')#>
<cfset array[#index#][2]=#listgetAt('#index#',2, ',')#>
</cfloop>
<cfdump var=#array#>
Run Code Online (Sandbox Code Playgroud)
奖金:
另外,如果有一些方法可以从ColdFusion中调用PHP文件,那么它将节省我很多时间,因为我已经在PHP中完成了整个脚本(这只是一小部分).我读到了关于ColdFusion自定义标签(标签<cf_php>对我来说非常适合),但是管理员说没有,因此我必须使用ColdFusion或者找到一些通过ColdFusion渲染PHP的方法.框架,JavaScript或<cfhttp>标签都是我认为可能有用的东西...如果您有任何想法让我知道.
实际上我认为你可以使用一维数组和arrayAppend进一步简化Henry的例子.
<cfset array=ArrayNew(1)>
<cfloop index="line" list="#csvfile#" delimiters="#chr(10)##chr(13)#">
<cfset arrayAppend(array, listToArray(line))>
</cfloop>
Run Code Online (Sandbox Code Playgroud)
coldfusion.runtime.Struct类型的标量值无法分配给二维ColdFusion数组.
仅供参考:原始代码是混合循环类型.随着<cfloop list="..">该index值是列表,如"A,B"(未行号)的元件.显然"a,b"不是预期的数字索引,因此是错误.
<!--- what the code is actually doing --->
<cfset array['a,b'][1]=#listgetAt('#index#',1, ',')#>
<cfset array['a,b'][2]=#listgetAt('#index#',2, ',')#>
<cfset array['c,d'][1]=#listgetAt('#index#',1, ',')#>
....
Run Code Online (Sandbox Code Playgroud)
与您的错误无关,这些#符号都不是必需的.代码将以任何方式工作,但编写更清晰:
<cfset array[lineNum][1]= listgetAt( index, 1, ',')>
Run Code Online (Sandbox Code Playgroud)
代替
<cfset array['#lineNum#'][1]=#listgetAt('#index#',1, ',')#>
Run Code Online (Sandbox Code Playgroud)