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.

asked Mar 10, 2016 at 16:26
\$\endgroup\$
2
  • \$\begingroup\$ Could you include all of the code? It's often hard for reviewers to review code when it's not all present. \$\endgroup\$ Commented Mar 10, 2016 at 16:41
  • \$\begingroup\$ @AlienHerbNite done \$\endgroup\$ Commented Mar 10, 2016 at 16:46

1 Answer 1

3
\$\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

answered Mar 10, 2016 at 16:50
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.