-1
ID  OID Title   Description Option  Price
01  01JAP   Japanese Model  Japanese Model of the Dave Smith    Japanese    "$3,000 "
02  02ENG   English Model   English Model of the dave Smith     english "$1,000 "

The txt file, example shown above is delimited with /t.

I need the first line to become the keys of my array, so the ID or OID for example

"01" => array(

"ID" => "01",
"OID" => "01JAP",
"title" => "Japaneses model",
"description" => "Japanese Model of the Dave Smith ",
"option" => "Japanese",
"price" => 3000

),

I've read and tried to use explode() but was also looking at fgetcsv(), but haven't found any examples putting it into an array with keys provided by the delimited txt file.

any help would be greatly appreciated!

n0rad
  • 82
  • 9

2 Answers2

0

To use the first line as the data keys, you need to read the line into a separate array and then on each line you read use array_combine() to add the keys to the values...

$fh = fopen("file.txt", "r");
$headers = fgetcsv($fh, 0, "\t");
$data = [];
while ( $row = fgetcsv ($fh, 0, "\t" ) ) {
    $data[] = array_combine($headers, $row);
}
fclose($fh);

This assumes the file is tab delimited, change the "\t" if you need another delimiter.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

You can try an explode on tabulation or spaces like :

explode("\t", $your_data)

You can tout parse with a regexp like :

preg_match_all("/([^\t])/", $your_data, $matches)

Get your data by simply :

$your_data = file_get_contents('your/file');
Chenille33
  • 93
  • 1
  • 1
  • 10