使用 PHP 将 CSV 值解析为地图

发布于 2024-12-06 01:28:27 字数 428 浏览 0 评论 0原文

如果我的文本文件是

Jack Jones;batter;100;
Bobby Brown;bowler;90;

我知道如何将它们放入数组中,但是如何将它们放入带有键的数组中?例如,在我读取文本文件后,我的数组将是 -

$player1 = ('name' => 'Jack Jones', 'skill' => 'batter', 'points' => '100');
$player2 = ('name' => 'Bobby Brown', 'skill' => 'bowler', 'points' => '90');

// therefore -
echo $player1['name'];  #This would output the name 'Jack Jones'

If my text file is

Jack Jones;batter;100;
Bobby Brown;bowler;90;

I know how to get them into an array but how would I get them into an array with Keys? For example, after I read the text file my array would be -

$player1 = ('name' => 'Jack Jones', 'skill' => 'batter', 'points' => '100');
$player2 = ('name' => 'Bobby Brown', 'skill' => 'bowler', 'points' => '90');

// therefore -
echo $player1['name'];  #This would output the name 'Jack Jones'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看海 2024-12-13 01:28:27

您不能直接这样做,因为文本文件中没有有关列名称的信息。但如果您知道如何获得此:

$player = (0 => 'Jack Jones', 1 => 'batter', 2 => '100');

并且列名称是固定的,您可以设置包含键的第二个数组:

$column_names = array('name','skill','points');

然后使用 array_combine 以获得您想要的结果:

$player = array_combine($column_names, $player);
echo $player['name'];  // 'Jack Jones'

you can't directly as there's no information about column names in the text file. but if you know how to get this:

$player = (0 => 'Jack Jones', 1 => 'batter', 2 => '100');

and the column names are fixed, you could set up a second array containing the keys:

$column_names = array('name','skill','points');

and then use array_combine to get your desired result:

$player = array_combine($column_names, $player);
echo $player['name'];  // 'Jack Jones'
温暖的光 2024-12-13 01:28:27

您如何解析 CSV 文件?
您使用 CsvReader 类吗?

在这种情况下,您可以执行以下操作:

Class CsvManager
{

function __construct()
{
$this->reader = new CsvReader();
$this->reader->SetCsvEncoding('yourEncoding');
$this->reader->SetDelimiter(';');
}

function Run($inString)
{
$this->reader->ParseCsv($inString,array(&$this, 'CsvCallback'), NULL);
}

function CsvCallback($inCsvArray)
{
$newArray = array();
$newArray['name'] = $inCsvArray[0];
$newArray['skill'] = $inCsvArray[1];
$newArray['points'] = $inCsvArray[2];
}

$file = pathToYourCsvFile.csv
$manager = new CsvManager();
$manager->Run($file);

希望这会有所帮助。

How do you parse your CSV file?
Do you use the Class CsvReader ?

In this case you can do something like:

Class CsvManager
{

function __construct()
{
$this->reader = new CsvReader();
$this->reader->SetCsvEncoding('yourEncoding');
$this->reader->SetDelimiter(';');
}

function Run($inString)
{
$this->reader->ParseCsv($inString,array(&$this, 'CsvCallback'), NULL);
}

function CsvCallback($inCsvArray)
{
$newArray = array();
$newArray['name'] = $inCsvArray[0];
$newArray['skill'] = $inCsvArray[1];
$newArray['points'] = $inCsvArray[2];
}

$file = pathToYourCsvFile.csv
$manager = new CsvManager();
$manager->Run($file);

hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文