memo

memo dayo.

TSVファイルを配列として読み込む


function tsv2ary ($file_name) {
$ary_return = array();

if (is_file($file_name) === False) {
return False;
}

$int_line = 0;
$ary_master = array();
$fp = fopen($file_name, 'rb');
while ( ($str_tmp = fgets($fp)) !== False) {
$str_tmp = str_replace("\r", '', $str_tmp);
$str_tmp = str_replace("\n", '', $str_tmp);
$ary_old = explode("\t", $str_tmp);
unset($str_tmp);

$int_line++;
if ($int_line === 1) {
$ary_master = $ary_old;
continue;
}

$ary_new = array();
for ($i=0; $i<count($ary_old); $i++) {
$ary_new[$ary_master[$i]] = trim_nl($ary_old[$i]);
}

$ary_return[] = $ary_new;
unset($ary_old);
unset($ary_new);
}

fclose($fp);
unset($fp);
unset($int_line);
unset($ary_master);

return $ary_return;
}