>我正在尝试使用 csv 上传批量数据。文件上传完美完成,但从csv获取数据失败并显示以下错误
array_map() 期望参数 1 是有效的回调,找不到类 '100'这是我在控制器中的代码
if ( isset($_FILES["userfile"]))
{
$this->load->library('upload_photo');
//Set the config
$config['upload_path'] = 'assets/uploads/csv/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '200';
$config['overwrite'] = TRUE;
$this->upload_photo->initialize($config);
if( ! $this->upload_photo->do_upload())
{
$error = $this->upload_photo->display_errors();
}
//If the upload success
$csv = $this->upload_photo->file_name;
$arrResult = array();
$handle = fopen("assets/uploads/csv/".$csv, "r");
if( $handle )
{
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$arrResult[] = $row;
}
fclose($handle);
}
$titles = array_shift($arrResult);
$keys = array('id', 'name', 'city','state');
$final = array();
foreach ( $arrResult as $key => $value )
{
$final[] = array_combine($keys, $value);
}
$rw=2;
foreach($final as $csv_em)
{
$rw++;
$id[] = $csv_em['id'];
$name[] = $csv_em['name'];
$city[] = $csv_em['city'];
$state[] = $csv_em['state'];
}
}
$ikeys = array('id', 'name', 'city','state');
$items = array();
foreach ( array_map($id, $name, $city, $state) as $ikey => $value)
{
$items[] = array_combine($ikeys, $value);
}
$this->employee_model->insertByCSV($items);
foreach ( array_map($id, $name, $city, $state) as $ikey => $value)
{
$items[] = array_combine($ikeys, $value);
}
试试这个
foreach ( array_map(function($a,$b,$c,$d){return array($a,$b,$c,$d);},$id, $name, $city, $state) as $ikey => $value)
{
$items[] = array_combine($ikeys, $value);
}
正如信息所说,你需要给出一个回调函数作为第一个参数array_map