我在 excel 文件中有一些值,我希望它们都是数组元素记住文件中还有其他数据。
我知道一种方法是将它们一个一个地复制并放入数组初始化语句中
一个示例列表,它只是整个列表的一部分
Bay Area
Greater Boston
Greater Chicago
Greater Dallas-Ft. Worth
Greater D.C.
Las Vegas
Greater Houston
Greater LA
Greater New York
Greater San Diego
Seattle
South Florida
当项目不多时,很容易用值初始化数组
$array= array('Bay Area '=>'Bay Area ','Greater Boston'=>'Greater Boston',....)
// and so on
但是我有 70-80 个项目,像上面那样初始化数组是非常繁琐的任务。
那么,伙计们,是否有任何替代方法或捷径来为数组分配值列表?
有没有自动生成数组的工具?
最佳答案
如果你将它们复制到一个文件中,每一行都有自己的行,你可以像这样在 php 中读取文件
$myArray = file('myTextFile');
//sets the keys to the array equal to the values
$myArray = array_combine($myArray, $myArray);
您可以将数据从 excel 导出到 CSV,然后像这样将文件读入 php:
$myArray = array();
if (($file = fopen("myTextFile", "r")) !== FALSE) {
while (($data = fgetcsv($file)) !== FALSE) {
foreach($data as $value) {
$myArray[$value] = $value;
}
}
fclose($handle);
}
关于PHP 数组生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5866876/