2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\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 Mar 10 '16 at 16:41
  • \$\begingroup\$ @AlienHerbNite done \$\endgroup\$ – DasSaffe Mar 10 '16 at 16:46
2
\$\begingroup\$

This has been answered here: https://stackoverflow.com/a/3000342/561731

array_combine

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.