Home > Enterprise >  PHP uploading CSV file and using first line as the KEYS instead of 0,1,2
PHP uploading CSV file and using first line as the KEYS instead of 0,1,2

Time:07-02

I am working on a CSV file upload function. The whole script is working fine and the way I am doing it is by eliminating the first line of the CSV file which is the heading and then using the data only to insert into the database. However, this adds a restriction for the CSV to be sorted always. I need to accept unsorted CSV too. For example, if a column name is in second column and of the CSV file then the array index becomes $arr[1]. Currently, I am using $arr[1] for inserting the values in the database and performing operations. This is bad. If the user uploads an unsorted CSV where name is in 4th column and say phone is in 2nd column where earlier I expected the name to be, then this will disrupt the whole operation. Therefore, how can I use the first heading line and use them as a key like $arr['name'] for performing the required operations?

My current code:

$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
fgetcsv($csvFile);

$arr = [];
while($data = fgetcsv($csvFile, 100, ",")){      
  $arr['id'] = $data[0]; // WANT TO USE $data['id'] FROM CSV FILE's FIRST LINE
  $arr['date'] = date('Y-m-d', strtotime($data[1])); // WANT TO USE $data['date'] FROM CSV FILE's FIRST LINE
  $arr['stock'] = $data[2]; // WANT TO USE $data['stock'] FROM CSV FILE's FIRST LINE
  $arr['price'] = $data[3]; // WANT TO USE $data['price'] FROM CSV FILE's FIRST LINE
  $ar[] = $arr;
}

fclose($csvFile);

How can I get the keys from the file and use it here in the code above?

UPDATE

I see that if I store the keys in array and print it like this

$csvFile = fopen($_FILES['file']['tmp_name'], 'r');
$getFileKeys = fgetcsv($csvFile); // STORED KEYS IN ARRAY
fgetcsv($csvFile);
print_r($getFileKeys);

then I get an array like this:

Array
(
    [0] => id
    [1] => date
    [2] => stock_name
    [3] => price
)

I need to write logic in such a way that if I have 4 variables one for each of the element above, then no matter if the index changes for any element, the variable will receive the same value dynamically.

CodePudding user response:

Since you already got the keys inside $getFileKeys variable you can simply use a for loop to loop through the array of keys and dynamically assign the indexes based upon the field.

$getFileKeys = fgetcsv($csvFile);

$keys = [];
for($i = 0; $i < count($getFileKeys); $i  ){
  if($getFileKeys[$i] == 'id'){
    $keys['id'] = $i;
  }else if($getFileKeys[$i] == 'date'){
    $keys['date'] = $i;
  }else if($getFileKeys[$i] == 'stock_name'){
    $keys['stock'] = $i;
  }else if($getFileKeys[$i] == 'price'){
    $keys['price'] = $i;
  }
}

while($getData = fgetcsv($csvFile, 100, ",")){
  $arr['id'] = $getData[$keys['id']];
  $arr['date'] = date('Y-m-d', strtotime($getData[$keys['date']]));
  $arr['stock'] = trim($getData[$keys['stock']]);
  $arr['price'] = $getData[$keys['price']];
  $ar[] = $arr;
}

The $keys array now dynamically stores the indexes for each key. Therefore, this now sorts the CSV file no matter at what order which column is placed in.

CodePudding user response:

You could make variables out of the first line, so that you can call them by her name.

$exampleLine=[
  'id' => 1
  'date' => '2022-07-01'
  'stock_name' => 'stock_name'
  '3' => 'price'
];
foreach($exampleLine as $name){
   $$name=$name;
 } 

This will give you the variables with appropriate content, so that $id contains 1, $date '2022-07-01' and so on.

to prevent different spellings, you could still use lcfirst and trim.

PHP-Manual

  • Related