我怎样才能读取使用codeigniter关联数组的excel文件?

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

在这里输入图像说明

调节器

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相关的出版商名称是Cust Angel

有些天才可以帮助我,提前致谢

任何查询有关的问题,请问我…

在这里,我的问题解决scheme

  $reader = PHPExcel_IOFactory::createReader('Excel2007'); $reader->setReadDataOnly(true); $file = isset($_FILES["uploadexcel"]['tmp_name']) ? $_FILES["uploadexcel"]['tmp_name'] : ''; $objPHPExcel = $reader->load($file); $objWorksheet = $objPHPExcel->getActiveSheet(); $header=true; if ($header) { $highestRow = $objWorksheet->getHighestRow(); $highestColumn = $objWorksheet->getHighestColumn(); $headingsArray = $objWorksheet->rangeToArray('A1:' . $highestColumn . '1', null, true, true, true); $headingsArray = $headingsArray[1]; $r = -1; $namedDataArray = array(); for ($row = 2; $row <= $highestRow; ++$row) { $dataRow = $objWorksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, null, true, true, true); if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) { ++$r; foreach ($headingsArray as $columnKey => $columnHeading) { $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey]; } } } } else { //excel sheet with no header $namedDataArray = $objWorksheet->toArray(null, true, true, true); } echo '<pre>';print_r($namedDataArray);exit; } 

输出: –

 Array ( [0] => Array ( [url] => gettingaway.com [category_1] => Finance [category_2] => General [language] => GA [f_n] => nofollow [publishers] => KAlw [cost] => 1245 [cost_currency] => [processing_time] => 12 [example] => testing [note] => Testing Value ) [1] => Array ( [url] => thebusbench.com [category_1] => Books & Writing [category_2] => Games [language] => GA [f_n] => nofollow [publishers] => Miclede [cost] => 12 [cost_currency] => USD [processing_time] => 11 [example] => fsdfsd fsdfd [note] => asd ) [2] => Array ( [url] => travelintelligence.net [category_1] => Finance [category_2] => Food & Drinks [language] => GA [f_n] => follow [publishers] => Micel [cost] => 123 [cost_currency] => [processing_time] => 25 [example] => fsdfsd fsdfd [note] => dfmlsdflsdk fjsdlfj sdlfjsdl fjld ) [3] => Array ( [url] => littleyayas.com [category_1] => Automotive [category_2] => General [language] => GA [f_n] => follow [publishers] => Cust Poo [cost] => 10200 [cost_currency] => [processing_time] => 5 [example] => asds [note] => adasdd ) [4] => Array ( [url] => tripwheeling.com [category_1] => Finance [category_2] => Finance [language] => GA [f_n] => follow [publishers] => Cust Angel [cost] => 152000 [cost_currency] => [processing_time] => 13 [example] => dasd [note] => adas ) [5] => Array ( [url] => gettingaway.com [category_1] => Home & Garden [category_2] => General [language] => EN [f_n] => nofollow [publishers] => abc [cost] => 250 [cost_currency] => USD [processing_time] => 10 [example] => asdasd [note] => asd ) )