I am a complete PHP/MySQL newb and would really appreciate some help storing the position of each value in an array to the appropriate DB table column. The value itself in the array corresponds to a column in the DB table.
In my php form processing page I have the array $rankArray, where each value corresponds with a column in my database table. What I want to do is save the position of of each value to its corresponding column. For example, if
$rankArray = [2,5,3,4,1]
it should store ‘1’ in column 2, ‘2’ in column 5, ‘3’ in column 3, etc.
I would do something like:
Now flipped should be an array in this order: [5,1,3,4,2] and your values can be inserted into the correct columns.
This works because array_flip returns an array where the keys and values are flipped, then ksort makes sure the array keys are sorted.