I have this function here:
<?php
public function assignData($data)
{
$aSqlData['company'] = $data[0];
$aSqlData['tx_trouserextend_contactperson'] = $data[1];
$aSqlData['username'] = $data[2];
$aSqlData['tx_trouserextend_commission'] = $data[3];
$aSqlData['tx_trouserextend_wincap_version'] = $data[4];
$aSqlData['tx_trouserextend_wincap_link'] = $data[5];
$aSqlData['tx_trouserextend_contract_due'] = $data[6];
$aSqlData['tx_trouserextend_pos_version'] = $data[7];
$aSqlData['tx_trouserextend_pos_link'] = $data[8];
$aSqlData['tx_trouserextend_quick_select_version'] = $data[9];
$aSqlData['tx_trouserextend_quick_select_link'] = $data[10];
$aSqlData['password'] = $data[11];
$this->userdata = $aSqlData;
}
This goes on for like 10 more items with this way of assigning. Is there maybe a better way to iterate through it? For instance, just adding the indices and run assign every numerical index to the corresponding associative array-key?
Even though it works, it is not that nice to maintain, imho. Ideas how to improve this? As you can imagine, if the csv-file grows and gets more fields, I'd have to add a line of code for every new column. Thought there would/could be a better way.
-
\$\begingroup\$ Could you include all of the code? It's often hard for reviewers to review code when it's not all present. \$\endgroup\$Ethan Bierlein– Ethan Bierlein2016年03月10日 16:41:34 +00:00Commented Mar 10, 2016 at 16:41
-
\$\begingroup\$ @AlienHerbNite done \$\endgroup\$DasSaffe– DasSaffe2016年03月10日 16:46:43 +00:00Commented Mar 10, 2016 at 16:46
1 Answer 1
This has been answered here: https://stackoverflow.com/a/3000342/561731
In your case:
$this->userdata = array_combine(['company','tx_trouserextend_contactperson',...], $data);
Can also just have anarray of $keys
to match to your data and then use array_combine