function
write_tabbed_file(
$filepath
,
$array
,
$save_keys
=false){
$content
=
''
;
reset(
$array
);
while
(list(
$key
,
$val
) = each(
$array
)){
$key
=
str_replace
(
"\t"
,
" "
,
$key
);
$val
=
str_replace
(
"\t"
,
" "
,
$val
);
if
(
$save_keys
){
$content
.=
$key
.
"\t"
; }
$content
.= (
is_array
(
$val
)) ? implode(
"\t"
,
$val
) :
$val
;
$content
.=
"\n"
;
}
if
(
file_exists
(
$filepath
) && !
is_writeable
(
$filepath
)){
return
false;
}
if
(
$fp
=
fopen
(
$filepath
,
'w+'
)){
fwrite(
$fp
,
$content
);
fclose(
$fp
);
}
else
{
return
false; }
return
true;
}
function
load_tabbed_file(
$filepath
,
$load_keys
=false){
$array
=
array
();
if
(!
file_exists
(
$filepath
)){
return
$array
; }
$content
= file(
$filepath
);
for
(
$x
=0;
$x
<
count
(
$content
);
$x
++){
if
(trim(
$content
[
$x
]) !=
''
){
$line
=
explode
(
"\t"
, trim(
$content
[
$x
]));
if
(
$load_keys
){
$key
=
array_shift
(
$line
);
$array
[
$key
] =
$line
;
}
else
{
$array
[] =
$line
; }
}
}
return
$array
;
}
$array
=
array
(
'line1'
=>
array
(
'data-1-1'
,
'data-1-2'
,
'data-1-3'
),
'line2'
=>
array
(
'data-2-1'
,
'data-2-2'
,
'data-2-3'
),
'line3'
=>
array
(
'data-3-1'
,
'data-3-2'
,
'data-3-3'
),
'line4'
=>
'foobar'
,
'line5'
=>
'hello world'
);
write_tabbed_file(
'data.txt'
,
$array
, true);
$reloaded_array
= load_tabbed_file(
'data.txt'
,true);
print_r(
$reloaded_array
);