gpt4 book ai didi

php - 如何在php中将数组存储为键值对

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:03:58 25 4
gpt4 key购买 nike

我有一个数组 $result 如下

Array 
( [0] => Array (
[0] => Mr
[1] => vinay
[2] => hs
[3] => tester
[4] =>vinay.hs@abc.com
[5] => 909099
[6] => Yes )

[1] => Array (
[0] => Mr
[1] => Suresh
[2] => Kumar
[3] => tester
[4] => vinay.hs@abc.com
[5] => 809090
[6] => No )
).

我想把这个数组存储为

Array
([0]=>Array (
[title] => Mr
[firstname] => vinay
[lastname] => hs
[job_title] => tester
[email] =>vinay.hs@abc.com
[phone] => 909099
[is_employed] => Yes )

[1] => Array (
[title] => Mr
[firstname] => Suresh
[lastname] => Kumar
[job_title] => tester
[email] => vinay.hs@abc.com
[phone] => 809090
[is_employed] => No ) ).

告诉我怎么做

最佳答案

$fp = fopen('foo.csv', 'r');
$fields = fgetcsv($fp); // assumes fields are the first row in the file

// or $fields = array('title', 'firstname', 'lastname', 'job_title', 'email', 'phone', 'is_employed');

$records = array();
while ($record = fgetcsv($fp))
{
$records[] = array_combine($fields, $record);
}

显然它需要添加错误处理。

关于php - 如何在php中将数组存储为键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5630007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com