PHP 数组生成器

标签 php arrays

我在 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/

相关文章:

python - 使用 Numpy 生成并填充二维数组

php - 正则表达式不适用于密码?不知道为什么

java - 无法从数组中获取实际值,它只是显示 "NULL"

Java:一个 int 数组的差异

php - Laravel - undefined variable : modules

java - 在 Java 中实现鸽巢排序

C 数组提取

php - 如何使用 Goutte 提交表单并获取最终 URI?

javascript - 在 Google Maps Directions API 中使用 PHP 变量

php - 如何给 "echo"一个类?