Jump to content

Need help with flat file


agent0

Recommended Posts

Hello.

 

I am trying to find a php script that will read the contents of a flat file and then sort according to a certain value in the line.

 

The file contents appear like this:

string1 tab string2 tab string3 tab string4

string1 tab string2 tab string3 tab string4

etc

 

Tab is a tabbed space between the values in the line.

 

I am looking for a php script to read each line into a multidimensional array and sort it according to the string2 value and then print it into a table.

 

I have searched google for days now and can't seem to find any.

 

I am also a noob at php so any help is greatly appreciated.  Thank you.

Link to comment
Share on other sites

Make an associative array and use multisort

$array = array(
     array('first' => 'Tom', 'last' => 'Edmunds'),
        array('first' => 'Alex', 'last' => 'Castle'),
        array('first' => 'Marshall', 'last' => 'Albreght'));

foreach ($array as $key => $row) {
    $last[$key]  = $row['last'];
}
array_multisort($last, SORT_ASC, $array);
echo "<pre>";
print_r($array);

 

 

HTH

Teamatomic

Link to comment
Share on other sites

<?php
//define the input file
$file = "somefile.txt";

//Read the file into an array
$line = file($file);

//Process the file array into a multi-dimensional array
$data = array();
foreach($lines as $line)
{
    $data = explode("\t", $line);
}

//Function to sort the array by index 1
funtion sortTheArray($a, $b)
{
    if($a[1]==$b[1]) { return 0; }
    return ($a[1] > $b[1]) ? 1 : -1;
}

//Sort the array
usort($data, "sortTheArray");

//Create output
//========================
//Detemine columns
$columns = count($data[0]);
//Open table
echo "<table>\n";
//Column headers
echo "  <tr>\n";
for($col=0; $col<$columns; $col++)
{
    echo "    <th>Field {$col}</th>\n";
}
echo "  <tr>\n";
//output records
foreach($data as $record)
{
    echo "  <tr>\n";
    foreach($record as $field)
    {
        echo "    <td>{$field}</td>\n";
    }
    echo "  <tr>\n";
}
//Close table
echo "</table>\n";
?>

Link to comment
Share on other sites

Decided to make a snip for my library

make the flat file like so, the first line is the fields, that way you have a value to sort by and it will work for any number of fields.

 

first	last	gender	status
tom	james	male	adult
joe	blow	male	minor
alice	jones	female	adult
jammer	hammer	male	adult

 

Here is the function, at the bottom is the usage. Fairly easy to use and can be sorted by any field. Mods would be easy for adding a sort type or delimiter.

 

function sort_array($file_name,$sorter)
{
$lines=file("$file_name");
$k=array_shift($lines);
$k=trim($k);
$kk=explode("\t",$k);
$kc=0;
for($kc;$kc<=count($kk)-1;$kc++)
{
$keys[$kc]=$kk[$kc];
}

$i=0;
foreach($lines as $line)
{
$line=trim($line);
$a1=explode("\t",$line);
   $j=0;
  for($j;$j<=count($a1)-1;$j++)
  {
   $array[$i][$keys[$j]]=$a1[$j];
  }
$i++;
}

foreach ($array as $key => $row) {
    $sorted[$key]  = $row[$sorter];
}
array_multisort($sorted, SORT_ASC, $array);
return $array;
}

$fn='./test.txt';
// val1 is the file name/path
// val2 is the field to sort by
$sorted_array = sort_array($fn,'last');
echo "<pre>";
print_r($sorted_array);

 

 

HTH

Teamatomic

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.