php 如何在 While 循环中初始化关联数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5264284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:48:23  来源:igfitidea点击:

How to Initialize an Associative Array in a While Loop?

phparrayswhile-loopassociative-array

提问by Kevin

I am now doing a simple thing, I am going to read from a CSV file, that column A contains code such as "EN", column B contains specific name "English"..etc., and I want to read them into an associate array.

我现在正在做一件简单的事情,我要从 CSV 文件中读取,A 列包含诸如“EN”之类的代码,B 列包含特定名称“English”..等,我想将它们读入关联数组。

My current doing is like this:

我目前的做法是这样的:

  $handle = fopen("Languages.csv","r") or die("EPIC FAIL!");

  $languageArray = array(
  while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) 
  {
              $row[0] => $row[1],
  }
  )

But it actually complains about my syntax, so I am just wondering if there is a way to initialize my associate array by getting all the rows of my csv file, and put the first string(from column A) as the key, the second string(from column B)as the value?

但它实际上抱怨我的语法,所以我只是想知道是否有办法通过获取我的 csv 文件的所有行来初始化我的关联数组,并将第一个字符串(来自 A 列)作为键,第二个字符串(来自 B 列)作为值?

Thanks.

谢谢。

回答by BoltClock

Initialize it as empty first:

首先将其初始化为空:

$languageArray = array();

Then populate it using a separate while loop like this:

然后使用单独的 while 循环填充它,如下所示:

while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) 
{
    $languageArray[$row[0]] = $row[1];
}

PHP arrays/hashes have no fixed size and are thus mutable in all kinds of ways, so even if you initialize them as empty you can populate them however you want later.

PHP 数组/散列没有固定大小,因此在各种方面都是可变的,因此即使您将它们初始化为空,您也可以稍后随意填充它们。

回答by linepogl

Do it more conventionally:

更传统地做:

$languageArray = array();
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) 
{
          $languageArray[$row[0]] = $row[1];
}

回答by ChrisJ

First create the array:

首先创建数组:

$myArray = array();

Then to add a key => value binding:

然后添加一个键 => 值绑定:

$myArray[$key] = $value;

In your case, this should be something like (in the loop):

在您的情况下,这应该类似于(在循环中):

$myArray[$row[0]] = $row[1];

回答by Pascal MARTIN

What about first initializing an empty array, that will later be populated with your data :

首先初始化一个空数组如何,稍后将填充您的数据:

$languageArray = array();

Then, going through the CVS file line by line, using each row to put data into that array :

然后,逐行浏览 CVS 文件,使用每一行将数据放入该数组:

$handle = fopen("Languages.csv","r") or die("EPIC FAIL!");
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
    // $row[0] is the first column,
    // $row[1] is the second one.
    $languageArray[$row[0]] = $row[1];
}


You cannot declare an array, using code inside it's initialization : you have to act in two steps.


你不能声明一个数组,在它的初始化中使用代码:你必须分两步操作。