Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7868959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:09:35+00:00 2026-06-03T01:09:35+00:00

Assuming a tab-separated values (TSV) file with a header line, how would one create

  • 0

Assuming a tab-separated values (TSV) file with a header line, how would one create a PHP array with the header fields as the key and the data fields as the data?

Assuming $txtArray contains all the lines in the file,

$hdrArray = explode( "\t", $txtArray[0]);
$i = 0;
foreach ($hdrArray as $hdr) {
  $heads[$hdr] = '';
  $headerNames[$i++] = $hdr;
}
for ($i = 1; $i < (count($txtArray) - 1); $i++ ) {
  $datArray = explode( "\t", $txtArray[$i]);
  if (count($datArray) > 1) {
    for($j = 0; $j < count($datArray); $j++) {
      $heads[$headerNames[$j]] = $datArray[$j];
    }
  }
  # process the line
}

I’ve got $heads containing field_name => field_data for all the fields in each line of the file. Is there a better way to code this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:09:38+00:00Added an answer on June 3, 2026 at 1:09 am

    What qualifies as ‘better’?

    You could use a regex split to make it a little more robust but if you have control over the source CSV you shouldn’t have to worry about dirty data.

    One obvious optimization I see is to cache the count() result.

    Use:

    for ($i = 1, $c=count($txtArray); $i < c - 1); $i++)
    

    Instead of:

    for ($i = 1; $i < (count($txtArray) - 1); $i++ )
    

    Every time you call count() it’s re-calculating the result. Doing the calculation once should be sufficient so you just save the result.

    I don’t see why you need:

    if (count($datArray) > 1)
    

    If you’re working with ‘clean’ data, it should have a fixed number of values-per-row so counting them and checking for none is unnecessary. To speed things up you could cache the row length by counting the number of rows in the header.

    After:

    $hdrArray = explode( "\t", $txtArray[0]);
    

    Do:

    $c2 = count($hdrArray);
    

    Then use it in the second for loop:

    for($j = 0; $j < $c2; $j++)
    

    If you do have to worry about empty rows it would probably be faster to search for an empty line and skip it in the loop.

    Like this:

    // skip the row if the $datArray contains an empty array
    if($datArray == array()) {
        continue;
    }
    $heads[$headerNames[$j]] = $datArray[$j];       
    

    Altogether you get:

    $hdrArray = explode( "\t", $txtArray[0]);
    $c2 = count($hdrArray);
    
    // it has an iterator variable...
    // I don't understand why you wouldn't use a for loop here
    $i = 0;
    foreach ($hdrArray as $hdr) {
        $heads[$hdr] = '';
        $headerNames[$i++] = $hdr;
    }
    
    for ($i = 1, $c = count($txtArray); $i < $c - 1; $i++) {
        $datArray = explode( "\t", $txtArray[$i]);
        for($j = 0; $j < $c2; $j++)
            // skip the row if the $datArray contains an empty array
            if($datArray == array()) {
                continue;
            }
            $heads[$headerNames[$j]] = $datArray[$j];
        }      
    }
    

    I’m assuming your first implementation worked, and the source data is actually CSV (ie fixed number of rows/columns.

    All I did was apply some simple (and common) optimizations to cut down on the number of unnecessary calculations. Pretty basic stuff that you get used to seeing after a while.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming you have only the URL to a file (hosted on the same server
Assuming that I have a typedef declared in my .h file as such: typedef
Assuming I have a table with one field called ID that stores 100 different
Let's say I have a UIButton in one tab view in my iPhone app,
In IE version 8/9 (possibly 7 also) when you create a new tab a
I create a new XIB file, drag a UITabBarController in it, and then try
The problem I have a tab delimited input file that looks like so: Variable
I am trying to create an application that works both on Samsung galaxy tab
Assuming both XML plist and JSON are delivered over http, gzipped, which one will
Assuming I'm using some graphic API which allows me to draw bezier curves by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.