我有要求我的csv文件有网址,我需要读取数组.csv文件有单列,有多行没有标题。我正在使用下面的代码阅读。但它没有返回输出我需要什么。
$handle = fopen($this->local_temp_folder.$file, 'r');
if(!$handle)
{
Model_Log::error('Cannot open uploaded file.');
die('Cannot open uploaded file.');
}
while (($data = fgetcsv($handle)) !== FALSE)
{
$num = count($data);
for ($c=0; $c < $num; $c++) {
$url_array[$c] = $data[$c];
}
}
print_r($url_array);exit;
Array
(
[0] => http://www.test.com/Fo/Finder/Digital_C/
http://www.test.com/Fo/Finder/orders/HighD/R56/
)
But i want it like the array as follows
Array
(
[0] => http://www.test.com/Fo/Finder/Digital_C/
[1] => http://www.test.com/Fo/Finder/orders/HighD/R56/
)
我用记事本检查了csv文件。在记事本中它看起来像这样 http://www.test.com/Fo/Finder/Digital_C/http://www.test.com/Fo/Finder/orders/HighD/R56/ 但是,当我用excel打开时,它显示为两行。
答案 0 :(得分:0)
$handle = fopen($this->local_temp_folder.$file, 'r');
if(!$handle)
{
Model_Log::error('Cannot open uploaded file.');
die('Cannot open uploaded file.');
}
while (($data = fgetcsv($handle)) !== FALSE)
{
$url_array[] = $data[0];
}
答案 1 :(得分:0)
试试这个,但如果你分享你的csv文件,最好能帮到你。
function read($file){
$data = array();
$csv_file = $file['folder'] . $file['name'];
$csvfile = fopen($csv_file, 'r');
if(!feof($csvfile)){
$i = 0;
while (!feof($csvfile)) {
$csv_data[] = fgetcsv($csvfile, 1000, ",");
$data[] = array(
'column1' => !empty($csv_data[$i][0])?$csv_data[$i][0]:'empty',
'column2' => !empty($csv_data[$i][1])?$csv_data[$i][1]:'empty',
'column3' => !empty($csv_data[$i][2])?$csv_data[$i][2]:'empty',
'column4' => !empty($csv_data[$i][3])?$csv_data[$i][3]:'empty',
'column5' => !empty($csv_data[$i][4])?$csv_data[$i][4]:'empty'
);
$i++;
}
}
return $data;
}
$data = read(array('folder'=>'','name'=>'file.csv'));//same folder
var_dump($data);