this code works great. The only thing i can figure out how to do is convert this to an array. I have 26 to 38 rows (that number varies) what i would like to do is have it turn each row of results into its own array.
$row[1] col1, col3, col4, col5, col8
$row[2] col1, col3, col4, col5, col8
$row[3] col1, col3, col4, col5, col8
I then want to be able to access each result like this: echo $row[1]; How can i do this and how do i know when the row ends?
<?php
$a = file_get_contents( "t_rac.txt" );
$a = str_replace( array( "
" , "\t" ) , array( "[NEW*LINE]" , "[tAbul*Ator]" ) , $a );
foreach( explode( "[NEW*LINE]" , $a ) AS $lines ) {
foreach( explode( "[tAbul*Ator]" , $lines ) AS $li ) {
if (strpos($li,',') !== false)
{
}
else {
echo $li;
}
echo "<br>";
}
}
?>
<?php
//use file to get array of lines
$arrayContent=file("t_rac.txt");
$returnArray=array();
$i=0;
foreach($arrayContent as $row){
$returnArray[$i++]=explode(",\t", $row);
}
?>
As a result you have multi-dimensional array. You can use it like:
echo $returnArray[0][3]; //prints single col5 value
echo implode(",\t", $returnArray[0]); //prints col1, col3, col4, col5, col8
EDIT Make sure you have tabs between cols.
t_rac.txt content:
$row[1] col1, col3, col4, col5, col8
$row[2] col1, col3, col4, col5, col8
$row[3] col1, col3, col4, col5, col8
this will build a php array with the numbers 1 through 10:
$a = array();
for ($i=1; $i<=10; $i++) {
$a[] = $i;
}
the []
notation automatically appends to the end of the array, no need for a subscript
note that php explode
returns an array, so you already have the array of cols. To omit the row[] from the beginning, use array_shift($a)
on the array