我上传了一个存储到数组$ allDataInSheet中的excel文件,并尝试在视图中print_r它。这是结果:
Array ([0] => 15220) Array ([0] => 15224)
控制器:
$file_data = $this->upload->data();
$file_path = FCPATH.'kirim_undangan/';
require_once APPPATH .'third_party/PHPExcel.php';
require_once APPPATH .'third_party/PHPExcel/IOFactory.php';
$inputFileName = $file_path;
$inputFileName;
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet); // Here get total count of row in that Excel sheet
$regex = "~\d{5}~";
$dataexcel = Array();
for($i=1;$i<=$arrayCount;$i++)
{
$dataexcel['to_name']=$allDataInSheet[$i]["A"];
$dataexcel['to_address']=$allDataInSheet[$i]["B"];
$dataexcel['to_phone']=$allDataInSheet[$i]["C"];
preg_match($regex, $allDataInSheet[$i]["B"], $result);
$dataexcel['to_zipcode'] = $result;
}
$data_user = array(
'request_Id' => $this->input->post('id'),
'email' => $this->input->post('email'),
'name' => $this->input->post('from_nama'),
'phone' => $this->input->post('from_phone'),
'status' => '0',
'unique_id' => uniqid()
);
$this->load->model('excel');
$this->excel->tambahuser($allDataInSheet,$data_user);
我的模特:
function tambahuser($allDataInSheet,$data_user)
{
$regex = "~\d{5}~";
$this->db->insert('request', $data_user);
$request_id = $this->db->insert_id();
foreach ($allDataInSheet as $key)
{
preg_match($regex, $key['B'], $result);
$data = array(
'request_id' => $request_id,
'from_name' => $this->input->post('from_nama'),
'from_phone' => $this->input->post('from_phone'),
//'from_address' => $this->input->post('from_address'),
'to_name' => $key['A'],
'to_phone' => $key['C'],
'to_address' => $key['B'],
'to_zipcode' => $result[0], //this is where the error comes
'tariff' => '0',
);
print_r($result);
$this->db->insert('excel', $data);
}
但我得Undefined Offset = 0
。
我做错了什么?因为[0]数组不是NULL;