gpt4 book ai didi

php - 如何使用 codeigniter 读取关联数组中的 excel 文件?

转载 作者:行者123 更新时间:2023-12-04 21:56:20 27 4
gpt4 key购买 nike

在这里,我使用 PHPexcel 库来读取文件。我的 excel 文件看起来像

enter image description here

Controller

class Home extends CI_Controller {
public function index() {
$this->load->library('excel');
$reader = PHPExcel_IOFactory::createReader('Excel2007');
$reader->setReadDataOnly(true);
$file = isset($_FILES["uploadexcel"]['tmp_name']) ? $_FILES["uploadexcel"]['tmp_name'] : '';
$excel = $reader->load($file);
foreach ($excel->getWorksheetIterator() as $worksheet) {
$worksheets = $worksheet->toArray();
}
echo '<pre>';print_r($worksheets);
}
}

当我上传 excel 文件时,我的数组如下:
 Array
(
[0] => Array
(
[0] => url
[1] => category_1
[2] => category_2
[3] => language
[4] => f_n
[5] => publishers
[6] => cost
[7] => cost_currency
[8] => processing_time
[9] => example
[10] => note
)
[1] => Array
(
[0] => gettingaway.com
[1] => 10
[2] => 14
[3] => GA
[4] => nofollow
[5] => 2
[6] => 1245
[7] =>
[8] => 12
[9] => testing
[10] => Testing Value
)


)

但是,我想要这样的数组:
Array
(
[0] => Array
(
[url] => gettingaway.com
[category_1] => 10
[category_2] => 14
[language] => GA
[f_n] => nofollow
[publishers] => Cust Angel
[cost] => 12
[cost_currency] => USD
[processing_time] => 12
[example] => example
[note] => note
)
[1] => Array
(
[url] => thebusbench.com
[category_1] => 5
[category_2] => 13
[language] => GA
[f_n] => nofollow
[publishers] => Cust Angel
[cost] => 12
[cost_currency] => USD
[processing_time] => 6
[example] => example
[note] => note
)
[2] => Array
(
[url] => travelintelligence.net
[category_1] => 6
[category_2] => 11
[language] => GA
[f_n] => nofollow
[publishers] => Cust Angel
[cost] => 12
[cost_currency] => USD
[processing_time] => 2
[example] => example
[note] => note
)
)

在上面的数组中,我的发布者键值为 2。即发布者的 ID 到我的数据库中。我希望将此相关的 id 发布者名称放入数组中。在我的数据库 id 2 中,相关的发布者名称是 天使 .

最佳答案

如果可以帮助到这里结束的人,我已经找到了一个更优雅的解决方案来解决问题:

foreach ($excel->getWorksheetIterator() as $worksheet) {
$worksheetArray = $worksheet->toArray();

// assuming the header is in the first line
$header = $worksheetArray[0];
unset($worksheetArray[0]);

$associativeArraySheet = array_map(static function ($row) use ($header) { return array_combine($header, $row); }, $worksheetArray);

print_r($associativeArraySheet);
}

关于php - 如何使用 codeigniter 读取关联数组中的 excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43995335/

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